home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / CALCFRAC.C < prev    next >
C/C++ Source or Header  |  1992-06-13  |  105KB  |  3,660 lines

  1. /*
  2. CALCFRAC.C contains the high level ("engine") code for calculating the
  3. fractal images (well, SOMEBODY had to do it!).
  4. Original author Tim Wegner, but just about ALL the authors have contributed
  5. SOME code to this routine at one time or another, or contributed to one of
  6. the many massive restructurings.
  7. This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  8. The following modules work very closely with CALCFRAC.C:
  9.   FRACTALS.C    the fractal-specific code for escape-time fractals.
  10.   FRACSUBR.C    assorted subroutines belonging mainly to calcfrac.
  11.   CALCMAND.ASM    fast Mandelbrot/Julia integer implementation
  12. Additional fractal-specific modules are also invoked from CALCFRAC:
  13.   LORENZ.C    engine level and fractal specific code for attractors.
  14.   JB.C        julibrot logic
  15.   PARSER.C    formula fractals
  16.   and more
  17.  -------------------------------------------------------------------- */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <float.h>
  23. #include <dos.h>
  24. #include <limits.h>
  25. #include "fractint.h"
  26. #include "fractype.h"
  27. #include "mpmath.h"
  28. #include "targa_lc.h"
  29.  
  30. /* routines in this module    */
  31.  
  32. void calcfrac_overlay(void);
  33. int  calcfract(void);
  34. /* the rest are called only within same overlay, thus don't need ENTER_OVLY */
  35. int  StandardFractal(void);
  36. int  calcmand(void);
  37. int  calcmandfp(void);
  38. int  plasma(void);
  39. int  diffusion(void);
  40. int  test(void);
  41. int  Bifurcation(void);
  42. int  BifurcVerhulst(void),LongBifurcVerhulst(void),BifurcLambda(void);
  43. int  LongBifurcLambda(void),BifurcAddSinPi(void),BifurcSetSinPi(void);
  44. int  BifurcStewart(void),LongBifurcStewart(void);
  45. int  popcorn(void);
  46.  
  47. static void perform_worklist(void);
  48. static int  OneOrTwoPass(void);
  49. static int  _fastcall StandardCalc(int);
  50. static int  _fastcall potential(double,int);
  51. static void decomposition(void);
  52. static int  bound_trace_main(void);
  53. static int  _fastcall boundary_trace(int,int);
  54. static int  _fastcall calc_xy(int,int);
  55. static int  _fastcall fillseg1(int,int,int,int);
  56. static int  _fastcall fillseg(int,int,int,int);
  57. static void _fastcall reverse_string(char *,char *,int);
  58. static int  solidguess(void);
  59. static int  _fastcall guessrow(int,int,int);
  60. static void _fastcall plotblock(int,int,int,int);
  61. static void _fastcall setsymmetry(int,int);
  62. static int  _fastcall xsym_split(int,int);
  63. static int  _fastcall ysym_split(int,int);
  64. static void set_Plasma_palette(void);
  65. static int _fastcall adjust(int xa,int ya,int x,int y,int xb,int yb);
  66. static void _fastcall subDivide(int x1,int y1,int x2,int y2);
  67. static int _fastcall new_adjust(int xa,int ya,int x,int y,int xb,int yb);
  68. static int _fastcall new_subD (int x1,int y1,int x2,int y2, int recur);
  69. static void verhulst(void);
  70. static void Bif_Period_Init(void);
  71. static int  _fastcall Bif_Periodic(int);
  72.  
  73. /**CJLT new function prototypes: */
  74. static int tesseral(void);
  75. static int _fastcall tesschkcol(int,int,int);
  76. static int _fastcall tesschkrow(int,int,int);
  77. static int _fastcall tesscol(int,int,int);
  78. static int _fastcall tessrow(int,int,int);
  79.  
  80. extern struct complex initorbit;
  81. extern char useinitorbit;
  82. struct lcomplex linitorbit;
  83. extern int showdot;
  84. extern unsigned int decoderline[];
  85. extern int overflow;
  86. long lmagnitud, llimit, llimit2, lclosenuff, l16triglim;
  87. struct complex init,tmp,old,new,saved;
  88. extern int biomorph,usr_biomorph;
  89. extern struct lcomplex linit;
  90. extern int basin;
  91. extern int cpu;
  92. extern char savename[80];   /* save files using this name */
  93. extern int resave_flag;
  94. extern int started_resaves;
  95. extern int dotmode;
  96.  
  97. int color, oldcolor, row, col, passes;
  98. int realcolor;
  99. int iterations, invert;
  100. double f_radius,f_xcenter, f_ycenter; /* for inversion */
  101. extern double far *dx0, far *dy0;
  102. extern double far *dx1, far *dy1;
  103.  
  104. extern int LogFlag;
  105. extern unsigned char far *LogTable;
  106. extern int rangeslen;
  107. extern int far *ranges;
  108.  
  109. void (_fastcall *plot)(int,int,int) = putcolor;
  110.  
  111. extern double inversion[];        /* inversion radius, f_xcenter, f_ycenter */
  112. extern int    xdots, ydots;        /* coordinates of dots on the screen  */
  113. extern int    sxdots,sydots;
  114. extern int    sxoffs,syoffs;
  115. extern int    colors;         /* maximum colors available */
  116. extern int    andcolor;        /* colors-1         */
  117. extern int    inside;         /* "inside" color to use    */
  118. extern int    fillcolor;         /* "fillcolor" color to use    */
  119. extern int    outside;        /* "outside" color to use   */
  120. extern int    finattract;
  121. double        min_orbit;        /* orbit value closest to origin */
  122. int        min_index;        /* iteration of min_orbit */
  123. extern int    maxit;            /* try this many iterations */
  124. extern int    fractype;        /* fractal type */
  125. extern char    stdcalcmode;        /* '1', '2', 'g', 'b' */
  126. extern int    debugflag;        /* for debugging purposes */
  127. extern    int    diskvideo;        /* for disk-video klooges */
  128. extern int    calc_status;        /* status of calculations */
  129. extern long    calctime;        /* total calc time for image */
  130.  
  131. extern int rflag, rseed;
  132. extern int decomp[];
  133. extern int distest,distestwidth;
  134.  
  135. extern double    param[];        /* parameters */
  136. extern int    potflag;        /* potential enabled? */
  137. extern double    potparam[];        /* potential parameters */
  138. extern int    pot16bit;        /* store 16bit continuous potential */
  139. extern long    far *lx0, far *ly0; /* X, Y points */
  140. extern long    far *lx1, far *ly1; /* X, Y points */
  141. extern long    fudge;            /* fudge factor (2**n) */
  142. extern int    bitshift;        /* bit shift for fudge */
  143. extern long    delmin;         /* for periodicity checking */
  144.  
  145. extern double xxmin,xxmax,yymin,yymax,xx3rd,yy3rd; /* corners */
  146. extern long   xmin, xmax, ymin, ymax;           /* integer equivs */
  147. extern long   delx,dely;               /* X, Y increments */
  148. extern double delxx,delxx2,delyy,delyy2;
  149. double deltaX, deltaY;
  150. double magnitude, rqlim, rqlim2, rqlim_save;
  151. extern struct complex parm,parm2;
  152. int (*calctype)();
  153. double closenuff;
  154. int pixelpi; /* value of pi in pixels */
  155. unsigned long lm;        /* magnitude limit (CALCMAND) */
  156. extern long linitx,linity;    /* in calcmand */
  157. extern unsigned long savedmask; /* in calcmand */
  158. extern int TranspSymmetry;
  159.  
  160. /* ORBIT variables */
  161. int    show_orbit;            /* flag to turn on and off */
  162. int    orbit_ptr;            /* pointer into save_orbit array */
  163. int far *save_orbit;            /* array to save orbit values */
  164. int    orbit_color=15;         /* XOR color */
  165.  
  166. int    ixstart, ixstop, iystart, iystop;    /* start, stop here */
  167. int    symmetry;       /* symmetry flag */
  168. int    reset_periodicity; /* nonzero if escape time pixel rtn to reset */
  169. int    kbdcount, max_kbdcount;    /* avoids checking keyboard too often */
  170.  
  171. extern    int    integerfractal;     /* TRUE if fractal uses integer math */
  172.  
  173. char far *resume_info = NULL;        /* pointer to resume info if allocated */
  174. int resuming;                /* nonzero if resuming after interrupt */
  175. int num_worklist;            /* resume worklist for standard engine */
  176. struct workliststuff worklist[MAXCALCWORK];
  177. int xxstart,xxstop;            /* these are same as worklist, */
  178. int yystart,yystop,yybegin;        /* declared as separate items  */
  179. int workpass,worksym;            /* for the sake of calcmand    */
  180.  
  181. extern long timer_interval;        /* timer(...) total */
  182.  
  183. extern void far *typespecific_workarea = NULL;
  184.  
  185. static double dem_delta, dem_width;    /* distance estimator variables */
  186. static double dem_toobig;
  187. #define DEM_BAILOUT 535.5  /* (pb: not sure if this is special or arbitrary) */
  188.  
  189. /* variables which must be visible for tab_display */
  190. int got_status; /* -1 if not, 0 for 1or2pass, 1 for ssg, 2 for btm, 3 for 3d */
  191. int curpass,totpasses;
  192. int currow,curcol;
  193.  
  194. /* static vars for solidguess & its subroutines */
  195. static int maxblock,halfblock;
  196. static int guessplot;            /* paint 1st pass row at a time?   */
  197. static int right_guess,bottom_guess;
  198. #define maxyblk 7    /* maxxblk*maxyblk*2 <= 4096, the size of "prefix" */
  199. #define maxxblk 202  /* each maxnblk is oversize by 2 for a "border" */
  200.              /* maxxblk defn must match fracsubr.c */
  201. /* next has a skip bit for each maxblock unit;
  202.    1st pass sets bit  [1]... off only if block's contents guessed;
  203.    at end of 1st pass [0]... bits are set if any surrounding block not guessed;
  204.    bits are numbered [..][y/16+1][x+1]&(1<<(y&15)) */
  205. extern unsigned int prefix[2][maxyblk][maxxblk]; /* common temp */
  206. /* size of next puts a limit of 2048 pixels across on solid guessing logic */
  207. extern char dstack[4096];        /* common temp, two put_line calls */
  208.  
  209. int    attractors;            /* number of finite attractors  */
  210. struct complex    attr[N_ATTR];        /* finite attractor vals (f.p)  */
  211. struct lcomplex lattr[N_ATTR];        /* finite attractor vals (int)  */
  212.  
  213. #ifndef sqr
  214. #define sqr(x) ((x)*(x))
  215. #endif
  216.  
  217. #ifndef lsqr
  218. #define lsqr(x) (multiply((x),(x),bitshift))
  219. #endif
  220.  
  221. /* -------------------------------------------------------------------- */
  222. /*        These variables are external for speed's sake only      */
  223. /* -------------------------------------------------------------------- */
  224. extern struct lcomplex lold,lnew,lparm,lparm2;     /* added "lold" */
  225.  
  226. int periodicitycheck;
  227. extern long ltempsqrx,ltempsqry;
  228. extern double tempsqrx,tempsqry;
  229. extern LCMPLX ltmp;
  230. extern int display3d;
  231.  
  232.  
  233. void calcfrac_overlay() { }    /* for restore_active_ovly */
  234.  
  235.  
  236. /******* calcfract - the top level routine for generating an image *******/
  237.  
  238. int calcfract()
  239. {
  240.    ENTER_OVLY(OVLY_CALCFRAC);
  241.  
  242.    attractors = 0;        /* default to no known finite attractors  */
  243.    display3d = 0;
  244.    basin = 0;
  245.  
  246.    init_misc();  /* set up some variables in parser.c */
  247.  
  248.    /* following delta values useful only for types with rotation disabled */
  249.    /* currently used only by bifurcation */
  250.    if (integerfractal)
  251.    {
  252.       distest = 0;
  253.       deltaX = (double)lx0[     1] / fudge - xxmin;
  254.       deltaY = yymax - (double)ly0[     1] / fudge;
  255.    }
  256.    else
  257.    {
  258.       deltaX = dx0[     1] - xxmin;
  259.       deltaY = yymax - dy0[     1];
  260.    }
  261.  
  262.    parm.x   = param[0];
  263.    parm.y   = param[1];
  264.    parm2.x  = param[2];
  265.    parm2.y  = param[3];
  266.  
  267.    if (LogFlag && colors < 16) {
  268.       static char far msg[]={"Need at least 16 colors to use logmap"};
  269.       stopmsg(0,msg);
  270.       LogFlag = 0;
  271.       }
  272.    if (LogFlag || rangeslen)
  273.       if (!(LogTable = farmemalloc((long)maxit + 1))) {
  274.       static char far msg[]={"Insufficient memory for logmap/ranges with this maxiter"};
  275.       stopmsg(0,msg);
  276.       }
  277.       else if (rangeslen) {
  278.      int i,k,l,m,numval,flip,altern;
  279.      i = k = l = 0;
  280.      while (i < rangeslen) {
  281.         m = flip = 0;
  282.         altern = 32767;
  283.         if ((numval = ranges[i++]) < 0) {
  284.            altern = ranges[i++];    /* sub-range iterations */
  285.            numval = ranges[i++];
  286.            }
  287.         if (numval > maxit || i >= rangeslen)
  288.            numval = maxit;
  289.         while (l <= numval)  {
  290.            LogTable[l++] = k + flip;
  291.            if (++m >= altern) {
  292.           flip ^= 1;        /* Alternate colors */
  293.           m = 0;
  294.           }
  295.            }
  296.         ++k;
  297.         if (altern != 32767) ++k;
  298.         }
  299.      }
  300.       else
  301.      SetupLogTable();
  302.  
  303.    lm = 4L << bitshift;         /* CALCMAND magnitude limit */
  304.  
  305.    /* ORBIT stuff */
  306.    save_orbit = (int far *)((double huge *)dx0 + 4*MAXPIXELS);
  307.    show_orbit = 0;
  308.    orbit_ptr = 0;
  309.    orbit_color = 15;
  310.    if(colors < 16)
  311.       orbit_color = 1;
  312.  
  313.    if(inversion[0] != 0.0)
  314.    {
  315.       f_radius      = inversion[0];
  316.       f_xcenter   = inversion[1];
  317.       f_ycenter   = inversion[2];
  318.  
  319.       if (inversion[0] == AUTOINVERT)  /*  auto calc radius 1/6 screen */
  320.      inversion[0] = f_radius = min(fabs(xxmax - xxmin),
  321.          fabs(yymax - yymin)) / 6.0;
  322.  
  323.       if (invert < 2 || inversion[1] == AUTOINVERT)  /* xcenter not already set */
  324.       {
  325.      inversion[1] = f_xcenter = (xxmin + xxmax) / 2.0;
  326.      if (fabs(f_xcenter) < fabs(xxmax-xxmin) / 100)
  327.         inversion[1] = f_xcenter = 0.0;
  328.       }
  329.  
  330.       if (invert < 3 || inversion[2] == AUTOINVERT)  /* ycenter not already set */
  331.       {
  332.      inversion[2] = f_ycenter = (yymin + yymax) / 2.0;
  333.      if (fabs(f_ycenter) < fabs(yymax-yymin) / 100)
  334.         inversion[2] = f_ycenter = 0.0;
  335.       }
  336.  
  337.       invert = 3; /* so values will not be changed if we come back */
  338.    }
  339.  
  340.    closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  341.    closenuff /= fudge;
  342.    rqlim_save = rqlim;
  343.    rqlim2 = sqrt(rqlim);
  344.    if (integerfractal)        /* for integer routines (lambda) */
  345.    {
  346.       lparm.x = parm.x * fudge;    /* real portion of Lambda */
  347.       lparm.y = parm.y * fudge;    /* imaginary portion of Lambda */
  348.       lparm2.x = parm2.x * fudge;  /* real portion of Lambda2 */
  349.       lparm2.y = parm2.y * fudge;  /* imaginary portion of Lambda2 */
  350.       llimit = rqlim * fudge;       /* stop if magnitude exceeds this */
  351.       if (llimit <= 0) llimit = 0x7fffffff; /* klooge for integer math */
  352.       llimit2 = rqlim2 * fudge;    /* stop if magnitude exceeds this */
  353.       lclosenuff = closenuff * fudge;    /* "close enough" value */
  354.       l16triglim = 8L<<16;       /* domain limit of fast trig functions */
  355.       linitorbit.x = initorbit.x * fudge;
  356.       linitorbit.y = initorbit.y * fudge;
  357.    }
  358.    resuming = (calc_status == 2);
  359.    if (!resuming) /* free resume_info memory if any is hanging around */
  360.    {
  361.       end_resume();
  362.       if (resave_flag) {
  363.      updatesavename(savename); /* do the pending increment */
  364.      resave_flag = started_resaves = 0;
  365.      }
  366.       calctime = 0;
  367.    }
  368.  
  369.    if (curfractalspecific->calctype != StandardFractal
  370.        && curfractalspecific->calctype != calcmand
  371.        && curfractalspecific->calctype != calcmandfp)
  372.    {
  373.       calctype = curfractalspecific->calctype; /* per_image can override */
  374.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  375.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  376.       iystart = ixstart = yystart = xxstart = yybegin = 0;
  377.       iystop = yystop = ydots -1;
  378.       ixstop = xxstop = xdots -1;
  379.       calc_status = 1; /* mark as in-progress */
  380.       distest = 0; /* only standard escape time engine supports distest */
  381.       /* per_image routine is run here */
  382.       if (curfractalspecific->per_image())
  383.       { /* not a stand-alone */
  384.      /* next two lines in case periodicity changed */
  385.      closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  386.      closenuff /= fudge;
  387.      lclosenuff = closenuff * fudge;    /* "close enough" value */
  388.      setsymmetry(symmetry,0);
  389.      timer(0,calctype); /* non-standard fractal engine */
  390.       }
  391.       if (check_key())
  392.       {
  393.      if (calc_status == 1) /* calctype didn't set this itself, */
  394.         calc_status = 3;   /* so mark it interrupted, non-resumable */
  395.       }
  396.       else
  397.      calc_status = 4; /* no key, so assume it completed */
  398.    }
  399.    else /* standard escape-time engine */
  400.       timer(0,(int (*)())perform_worklist);
  401.    calctime += timer_interval;
  402.  
  403.    if(LogTable)
  404.    {
  405.       farmemfree(LogTable);
  406.       LogTable = NULL;
  407.    }
  408.    if(typespecific_workarea)
  409.       free_workarea();
  410.  
  411.    EXIT_OVLY;
  412.    return((calc_status == 4) ? 0 : -1);
  413. }
  414.  
  415. /**************** general escape-time engine routines *********************/
  416.  
  417. static void perform_worklist()
  418. {
  419.    int i;
  420.    long tmplong; /* this temp must be signed */
  421.  
  422.    if (potflag && pot16bit)
  423.    {
  424.       stdcalcmode = '1'; /* force 1 pass */
  425.       if (resuming == 0)
  426.      pot_startdisk();
  427.    }
  428.    if (stdcalcmode == 'b' && (curfractalspecific->flags & NOTRACE))
  429.       stdcalcmode = '1';
  430.    if (stdcalcmode == 'g' && (curfractalspecific->flags & NOGUESS))
  431.       stdcalcmode = '1';
  432.  
  433.    /* default setup a new worklist */
  434.    num_worklist = 1;
  435.    worklist[0].xxstart = 0;
  436.    worklist[0].yystart = worklist[0].yybegin = 0;
  437.    worklist[0].xxstop = xdots - 1;
  438.    worklist[0].yystop = ydots - 1;
  439.    worklist[0].pass = worklist[0].sym = 0;
  440.    if (resuming) /* restore worklist, if we can't the above will stay in place */
  441.    {
  442.       start_resume();
  443.       get_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  444.       end_resume();
  445.    }
  446.  
  447.    if (distest) /* setup stuff for distance estimator */
  448.    {
  449.       double ftemp,ftemp2;
  450.       dem_delta = sqr(delxx) + sqr(delyy2);
  451.       if ((ftemp = sqr(delyy) + sqr(delxx2)) > dem_delta)
  452.      dem_delta = ftemp;
  453.       if (distestwidth == 0)
  454.      distestwidth = 71;
  455.       ftemp = distestwidth;
  456.       dem_delta *= sqr(ftemp)/10000; /* multiply by thickness desired */
  457.       dem_width = ( sqrt( sqr(xxmax-xxmin) + sqr(xx3rd-xxmin) ) * ydots/xdots
  458.       + sqrt( sqr(yymax-yymin) + sqr(yy3rd-yymin) ) ) / distest;
  459.       ftemp = (rqlim < DEM_BAILOUT) ? DEM_BAILOUT : rqlim;
  460.       ftemp += 3; /* bailout plus just a bit */
  461.       ftemp2 = log(ftemp);
  462.       dem_toobig = sqr(ftemp) * sqr(ftemp2) * 4 / dem_delta;
  463.    }
  464.  
  465.    while (num_worklist > 0)
  466.    {
  467.       calctype = curfractalspecific->calctype; /* per_image can override */
  468.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  469.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  470.  
  471.       /* pull top entry off worklist */
  472.       ixstart = xxstart = worklist[0].xxstart;
  473.       ixstop  = xxstop    = worklist[0].xxstop;
  474.       iystart = yystart = worklist[0].yystart;
  475.       iystop  = yystop    = worklist[0].yystop;
  476.       yybegin  = worklist[0].yybegin;
  477.       workpass = worklist[0].pass;
  478.       worksym  = worklist[0].sym;
  479.       --num_worklist;
  480.       for (i=0; i<num_worklist; ++i)
  481.      worklist[i] = worklist[i+1];
  482.  
  483.       calc_status = 1; /* mark as in-progress */
  484.  
  485.       curfractalspecific->per_image();
  486.  
  487.       /* some common initialization for escape-time pixel level routines */
  488.       closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  489.       closenuff /= fudge;
  490.       lclosenuff = closenuff * fudge;    /* "close enough" value */
  491.       kbdcount=max_kbdcount;
  492.       /* savedmask is for calcmand's periodicity checking */
  493.       savedmask = 0xC0000000; /* top 2 bits on */
  494.       tmplong = (delmin >> abs(periodicitycheck)) | 1;
  495.       while (tmplong > 0) /* while top bit not on */
  496.       {
  497.      tmplong <<= 1;
  498.      savedmask = (savedmask >> 1) | 0x80000000;
  499.       }
  500.  
  501.       setsymmetry(symmetry,1);
  502.  
  503.       /* call the appropriate escape-time engine */
  504.       switch (stdcalcmode)
  505.       {
  506.       case 't':
  507.      tesseral();
  508.      break;
  509.       case 'b':
  510.      bound_trace_main();
  511.      break;
  512.       case 'g':
  513.      solidguess();
  514.      break;
  515.       default:
  516.      OneOrTwoPass();
  517.       }
  518.  
  519.       if (check_key()) /* interrupted? */
  520.      break;
  521.    }
  522.  
  523.    if (num_worklist > 0)
  524.    {  /* interrupted, resumable */
  525.       alloc_resume(sizeof(worklist)+10,1);
  526.       put_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  527.    }
  528.    else
  529.       calc_status = 4; /* completed */
  530. }
  531.  
  532. static int OneOrTwoPass()
  533. {
  534.    int i;
  535.    totpasses = 1;
  536.    if (stdcalcmode == '2') totpasses = 2;
  537.    if (stdcalcmode == '2' && workpass == 0) /* do 1st pass of two */
  538.    {
  539.       if (StandardCalc(1) == -1)
  540.       {
  541.      add_worklist(xxstart,xxstop,yystart,yystop,row,0,worksym);
  542.      return(-1);
  543.       }
  544.       if (num_worklist > 0) /* worklist not empty, defer 2nd pass */
  545.       {
  546.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  547.      return(0);
  548.       }
  549.       workpass = 1;
  550.       yybegin = yystart;
  551.    }
  552.    /* second or only pass */
  553.    if (StandardCalc(2) == -1)
  554.    {
  555.       i = yystop;
  556.       if (iystop != yystop) /* must be due to symmetry */
  557.      i -= row - iystart;
  558.       add_worklist(xxstart,xxstop,row,i,row,workpass,worksym);
  559.       return(-1);
  560.    }
  561.    return(0);
  562. }
  563.  
  564. static int _fastcall StandardCalc(int passnum)
  565. {
  566.    got_status = 0;
  567.    curpass = passnum;
  568.    row = yybegin;
  569.    while (row <= iystop)
  570.    {
  571.       currow = row;
  572.       reset_periodicity = 1;
  573.       col = ixstart;
  574.       while (col <= ixstop)
  575.       {
  576.          if(showdot>0)
  577.             (*plot) (col, row, showdot&(colors-1));
  578.  
  579.      /* on 2nd pass of two, skip even pts */
  580.      if (passnum == 1 || stdcalcmode == '1' || (row&1) != 0 || (col&1) != 0)
  581.      {
  582.         if ((*calctype)() == -1) /* StandardFractal(), calcmand() or calcmandfp() */
  583.            return(-1); /* interrupted */
  584.         reset_periodicity = 0;
  585.         if (passnum == 1) /* first pass, copy pixel and bump col */
  586.         {
  587.            if ((row&1) == 0 && row < iystop)
  588.            {
  589.           (*plot)(col,row+1,color);
  590.           if ((col&1) == 0 && col < ixstop)
  591.              (*plot)(col+1,row+1,color);
  592.            }
  593.            if ((col&1) == 0 && col < ixstop)
  594.           (*plot)(++col,row,color);
  595.         }
  596.      }
  597.      ++col;
  598.       }
  599.       if (passnum == 1 && (row&1) == 0)
  600.      ++row;
  601.       ++row;
  602.    }
  603.    return(0);
  604. }
  605.  
  606. int calcmand()        /* fast per pixel 1/2/b/g, called with row & col set */
  607. {
  608.    /* setup values from far array to avoid using es reg in calcmand.asm */
  609.    linitx = lx0[col] + lx1[row];
  610.    linity = ly0[row] + ly1[col];
  611.    if (calcmandasm() >= 0)
  612.    {
  613.       if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  614.       && (realcolor < maxit || (inside < 0 && color == maxit)))
  615.      color = LogTable[min(color, maxit)];
  616.       if (color >= colors) /* don't use color 0 unless from inside/outside */
  617.      if (colors < 16)
  618.         color &= andcolor;
  619.      else
  620.         color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  621.       (*plot) (col, row, color);
  622.    }
  623.    return (color);
  624. }
  625.  
  626. /************************************************************************/
  627. /* added by Wes Loewer - sort of a floating point version of calcmand() */
  628. /* can also handle invert, any rqlim, potflag, zmag, epsilon cross,     */
  629. /* and all the current outside options    -Wes Loewer 11/03/91          */
  630. /************************************************************************/
  631. int calcmandfp()
  632. {
  633.    if(invert)
  634.       invertz2(&init);
  635.    else
  636.    {
  637.       init.y = dy0[row]+dy1[col];
  638.       init.x = dx0[col]+dx1[row];
  639.    }
  640.    if (calcmandfpasm() >= 0)
  641.    {
  642.       if (potflag)
  643.      color = potential(magnitude, realcolor);
  644.      if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  645.           && (realcolor < maxit || (inside < 0 && color == maxit)))
  646.         color = LogTable[min(color,maxit)];
  647.      if (color >= colors) /* don't use color 0 unless from inside/outside */
  648.         if (colors < 16)
  649.            color &= andcolor;
  650.         else
  651.            color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  652.       (*plot) (col, row, color);
  653.    }
  654.    return (color);
  655. }
  656.  
  657. #define green 2
  658. #define yellow 6
  659. int StandardFractal()    /* per pixel 1/2/b/g, called with row & col set */
  660. {
  661.    double tantable[16];
  662.    int hooper;
  663.    double close;
  664.    long lclose;
  665.    int caught_a_cycle;
  666.    int savedand, savedincr;    /* for periodicity checking */
  667.    struct lcomplex lsaved;
  668.    int i, attracted;
  669.    struct lcomplex lat;
  670.    struct complex  at;
  671.    struct complex deriv;
  672.    int dem_color;
  673.    struct complex dem_new;
  674.    close = .01;
  675.    lclose = close*fudge;
  676.    if(inside == -101)
  677.    {
  678.       int i;
  679.       for(i=0;i<16;i++)
  680.      tantable[i] = 0.0;
  681.    }
  682.    else if(inside == -100)
  683.    {
  684.       close = .01;
  685.       lclose = close*fudge;
  686.    }
  687.    if (periodicitycheck == 0 || inside == -59 || inside == -101)
  688.       oldcolor = 32767;     /* don't check periodicity at all */
  689.    else if (reset_periodicity)
  690.       oldcolor = 250;        /* don't check periodicity 1st 250 iterations */
  691.  
  692.    /* really fractal specific, but we'll leave it here */
  693.    if (!integerfractal)
  694.    {
  695.       if (useinitorbit == 1)
  696.      saved = initorbit;
  697.       else {
  698.      saved.x = 0;
  699.      saved.y = 0;
  700.       }
  701.       init.y = dy0[row] + dy1[col];
  702.       if (distest)
  703.       {
  704.      rqlim = rqlim_save;          /* start with regular bailout */
  705.      if (distest != 1 || colors == 2) /* not doing regular outside colors */
  706.         if (rqlim < DEM_BAILOUT)      /* so go straight for dem bailout */
  707.            rqlim = DEM_BAILOUT;
  708.      deriv.x = 1;
  709.      deriv.y = 0;
  710.      magnitude = 0;
  711.      dem_color = -1;
  712.       }
  713.    }
  714.    else
  715.    {
  716.       if (useinitorbit == 1)
  717.      lsaved = linitorbit;
  718.       else {
  719.      lsaved.x = 0;
  720.      lsaved.y = 0;
  721.       }
  722.       linit.y = ly0[row] + ly1[col];
  723.    }
  724.    orbit_ptr = 0;
  725.    color = 0;
  726.    if(fractype==JULIAFP || fractype==JULIA)
  727.       color = -1;
  728.    caught_a_cycle = 0;
  729.    savedand = 1;        /* begin checking every other cycle */
  730.    savedincr = 1;        /* start checking the very first time */
  731.  
  732.    if (inside <= -60 && inside >= -61)
  733.    {
  734.       magnitude = lmagnitud = 0;
  735.       min_orbit = 100000.0;
  736.    }
  737.    overflow = 0;        /* reset integer math overflow flag */
  738.  
  739.    curfractalspecific->per_pixel(); /* initialize the calculations */
  740.  
  741.    attracted = FALSE;
  742.    while (++color < maxit)
  743.    {
  744.       if(showdot>0)
  745.          (*plot) (col, row, showdot&(colors-1));
  746.  
  747.       /* calculation of one orbit goes here */
  748.       /* input in "old" -- output in "new" */
  749.  
  750.       if (distest)
  751.       {
  752.      double ftemp;
  753.      /* Distance estimator for points near Mandelbrot set */
  754.      /* Original code by Phil Wilson, hacked around by PB */
  755.      /* Algorithms from Peitgen & Saupe, Science of Fractal Images, p.198 */
  756.      ftemp     = 2 * (old.x * deriv.x - old.y * deriv.y) + 1;
  757.      deriv.y = 2 * (old.y * deriv.x + old.x * deriv.y);
  758.      deriv.x = ftemp;
  759.      if (sqr(deriv.x)+sqr(deriv.y) > dem_toobig)
  760.         break;
  761.      /* if above exit taken, the later test vs dem_delta will place this
  762.             point on the boundary, because mag(old)<bailout just now */
  763.      if (curfractalspecific->orbitcalc())
  764.      {
  765.         if (dem_color < 0)          /* note "regular" color for later */
  766.         {
  767.            dem_color = color;
  768.            dem_new = new;
  769.         }
  770.         if (rqlim >= DEM_BAILOUT  /* exit if past real bailout */
  771.         || magnitude >= (rqlim = DEM_BAILOUT) /* reset to real bailout */
  772.         || magnitude == 0)     /* exit if type doesn't "floatbailout" */
  773.            break;
  774.         old = new;              /* carry on till past real bailout */
  775.      }
  776.       }
  777.       else /* the usual case */
  778.      if (curfractalspecific->orbitcalc() && inside != -101)
  779.         break;
  780.       if (show_orbit)
  781.      if (!integerfractal)
  782.         plot_orbit(new.x, new.y, -1);
  783.      else
  784.         iplot_orbit(lnew.x, lnew.y, -1);
  785.       if(inside == -101)
  786.       {
  787.      if(0 < color && color < 16)
  788.      {
  789.      if (integerfractal)
  790.      {
  791.            new.x = lnew.x;
  792.            new.x /= fudge;
  793.            new.y = lnew.y;
  794.            new.y /= fudge;
  795.         }
  796.         tantable[color-1] = new.y/(new.x+.000001);
  797.      }
  798.       }
  799.       else if(inside == -100)
  800.       {
  801.      hooper = 0;
  802.      if(integerfractal)
  803.      {
  804.         if(labs(lnew.x) < lclose)
  805.         {
  806.            hooper = 1; /* close to y axis */
  807.            goto plot_inside;
  808.         }
  809.         else if(labs(lnew.y) < lclose)
  810.         {
  811.            hooper = 2; /* close to x axis */
  812.            goto plot_inside;
  813.         }
  814.      }
  815.      else
  816.      {
  817.         if(fabs(new.x) < close)
  818.         {
  819.            hooper = 1; /* close to y axis */
  820.            goto plot_inside;
  821.         }
  822.         else if(fabs(new.y) < close)
  823.         {
  824.            hooper = 2; /* close to x axis */
  825.            goto plot_inside;
  826.         }
  827.      }
  828.       }
  829.       else if (inside <= -60 && inside >= -61)
  830.       {
  831.      if (integerfractal)
  832.      {
  833.         if (lmagnitud == 0)
  834.            lmagnitud = lsqr(lnew.x) + lsqr(lnew.y);
  835.         magnitude = lmagnitud;
  836.         magnitude = magnitude / fudge;
  837.      }
  838.      else
  839.         if (magnitude == 0.0)
  840.            magnitude = sqr(new.x) + sqr(new.y);
  841.      if (magnitude < min_orbit)
  842.      {
  843.         min_orbit = magnitude;
  844.         min_index = color + 1;
  845.      }
  846.       }
  847.  
  848.  
  849.       if (attractors > 0)    /* finite attractor in the list   */
  850.       {             /* NOTE: Integer code is UNTESTED */
  851.      if (integerfractal)
  852.      {
  853.         for (i = 0; i < attractors; i++)
  854.         {
  855.         lat.x = lnew.x - lattr[i].x;
  856.         lat.x = lsqr(lat.x);
  857.         if (lat.x < l_at_rad)
  858.         {
  859.            lat.y = lnew.y - lattr[i].y;
  860.            lat.y = lsqr(lat.y);
  861.            if (lat.y < l_at_rad)
  862.            {
  863.               if ((lat.x + lat.y) < l_at_rad)
  864.               {
  865.              attracted = TRUE;
  866.              break;
  867.               }
  868.            }
  869.         }
  870.         }
  871.      }
  872.      else
  873.      {
  874.         for (i = 0; i < attractors; i++)
  875.         {
  876.         at.x = new.x - attr[i].x;
  877.         at.x = sqr(at.x);
  878.         if (at.x < f_at_rad)
  879.         {
  880.            at.y = new.y - attr[i].y;
  881.            at.y = sqr(at.y);
  882.            if ( at.y < f_at_rad)
  883.            {
  884.               if ((at.x + at.y) < f_at_rad)
  885.               {
  886.              attracted = TRUE;
  887.              break;
  888.               }
  889.            }
  890.         }
  891.         }
  892.      }
  893.      if (attracted)
  894.         break;        /* AHA! Eaten by an attractor */
  895.       }
  896.  
  897.       if (color > oldcolor)    /* check periodicity */
  898.       {
  899.      if ((color & savedand) == 0)         /* time to save a new value */
  900.      {
  901.         if (!integerfractal)
  902.            saved = new;  /* floating pt fractals */
  903.         else
  904.            lsaved = lnew;/* integer fractals */
  905.         if (--savedincr == 0)    /* time to lengthen the periodicity? */
  906.         {
  907.            savedand = (savedand << 1) + 1;         /* longer periodicity */
  908.            savedincr = 4;/* restart counter */
  909.         }
  910.      }
  911.      else             /* check against an old save */
  912.      {
  913.         if (!integerfractal)     /* floating-pt periodicity chk */
  914.         {
  915.            if (fabs(saved.x - new.x) < closenuff)
  916.           if (fabs(saved.y - new.y) < closenuff)
  917.           {
  918.              caught_a_cycle = 1;
  919.              color = maxit - 1;
  920.           }
  921.         }
  922.         else         /* integer periodicity check */
  923.         {
  924.            if (labs(lsaved.x - lnew.x) < lclosenuff)
  925.           if (labs(lsaved.y - lnew.y) < lclosenuff)
  926.           {
  927.              caught_a_cycle = 1;
  928.              color = maxit - 1;
  929.           }
  930.         }
  931.      }
  932.       }
  933.    }
  934.  
  935.    if (show_orbit)
  936.       scrub_orbit();
  937.  
  938.    realcolor = color;        /* save this before we start adjusting it */
  939.    if (color >= maxit)
  940.       oldcolor = 0;        /* check periodicity immediately next time */
  941.    else
  942.    {
  943.       oldcolor = color + 10;    /* check when past this + 10 next time */
  944.       if (color == 0)
  945.      color = 1;        /* needed to make same as calcmand */
  946.    }
  947.  
  948.    if (potflag)
  949.    {
  950.       if (integerfractal)    /* adjust integer fractals */
  951.       {
  952.      new.x = ((double)lnew.x) / fudge;
  953.      new.y = ((double)lnew.y) / fudge;
  954.       }
  955.       magnitude = sqr(new.x) + sqr(new.y);
  956.       color = potential(magnitude, color);
  957.       if (LogTable)
  958.      color = LogTable[min(color,maxit)];
  959.       goto plot_pixel;        /* skip any other adjustments */
  960.    }
  961.  
  962.    if (color >= maxit)        /* an "inside" point */
  963.       goto plot_inside;     /* distest, decomp, biomorph don't apply */
  964.  
  965.  
  966.    if (outside < -1)  /* these options by Richard Hughes modified by TW */
  967.    {
  968.       if (integerfractal)
  969.       {
  970.      new.x = ((double)lnew.x) / fudge;
  971.      new.y = ((double)lnew.y) / fudge;
  972.       }
  973.       /* Add 7 to overcome negative values on the MANDEL    */
  974.       if (outside == -2)         /* "real" */
  975.      color += new.x + 7;
  976.       else if (outside == -3)         /* "imag" */
  977.      color += new.y + 7;
  978.       else if (outside == -4  && new.y)  /* "mult" */
  979.       color *= (new.x/new.y);
  980.       else if (outside == -5)         /* "sum" */
  981.       color += new.x + new.y;
  982.  
  983.       /* eliminate negative colors & wrap arounds */
  984.       if (color < 0 || color > maxit)
  985.       color = 0;
  986.    }
  987.  
  988.    if (distest)
  989.    {
  990.       double dist,temp;
  991.       dist = sqr(new.x) + sqr(new.y);
  992.       temp = log(dist);
  993.       dist = dist * sqr(temp) / ( sqr(deriv.x) + sqr(deriv.y) );
  994.       if (dist < dem_delta)    /* point is on the edge */
  995.       {
  996.      if (distest > 0)
  997.         goto plot_inside;    /* show it as an inside point */
  998.      color = 0 - distest;    /* show boundary as specified color */
  999.      goto plot_pixel;    /* no further adjustments apply */
  1000.       }
  1001.       if (colors == 2)
  1002.       {
  1003.      color = !inside;    /* the only useful distest 2 color use */
  1004.      goto plot_pixel;    /* no further adjustments apply */
  1005.       }
  1006.       if (distest > 1)        /* pick color based on distance */
  1007.       {
  1008.      color = sqrt(dist / dem_width + 1);
  1009.      goto plot_pixel;    /* no further adjustments apply */
  1010.       }
  1011.       color = dem_color;    /* use pixel's "regular" color */
  1012.       new = dem_new;
  1013.    }
  1014.  
  1015.    if (decomp[0] > 0)
  1016.       decomposition();
  1017.    else if (biomorph != -1)
  1018.    {
  1019.       if (integerfractal)
  1020.       {
  1021.      if (labs(lnew.x) < llimit2 || labs(lnew.y) < llimit2)
  1022.         color = biomorph;
  1023.       }
  1024.       else
  1025.      if (fabs(new.x) < rqlim2 || fabs(new.y) < rqlim2)
  1026.         color = biomorph;
  1027.    }
  1028.  
  1029.    if (outside >= 0 && attracted == FALSE) /* merge escape-time stripes */
  1030.       color = outside;
  1031.    else if (LogTable)
  1032.       color = LogTable[min(color,maxit)];
  1033.    goto plot_pixel;
  1034.  
  1035.    plot_inside: /* we're "inside" */
  1036.    if (periodicitycheck < 0 && caught_a_cycle)
  1037.       color = 7;           /* show periodicity */
  1038.    else if (inside >= 0)
  1039.       color = inside;           /* set to specified color, ignore logpal */
  1040.    else
  1041.    {
  1042.       if(inside == -101)
  1043.       {
  1044.      int i;
  1045.      double diff;
  1046.      color = 0;
  1047.      for(i=1;i<16;i++)
  1048.      {
  1049.         diff = tantable[0] - tantable[i];
  1050.         if(fabs(diff) < .05)
  1051.         {
  1052.            color = i;
  1053.            break;
  1054.         }
  1055.      }
  1056.       }
  1057.       else if(inside == -100)
  1058.       {
  1059.      if(hooper==1)
  1060.         color = green;
  1061.      else if(hooper==2)
  1062.         color = yellow;
  1063.      else
  1064.         color = maxit;
  1065.      if (show_orbit)
  1066.         scrub_orbit();
  1067.       }
  1068.       else if (inside == -60)
  1069.      color = sqrt(min_orbit) * 75;
  1070.       else if (inside == -61)
  1071.      color = min_index;
  1072.       else if (inside == -59)
  1073.       {
  1074.      if (integerfractal)
  1075.      {
  1076.         /*
  1077.         new.x = ((double)lnew.x) / fudge;
  1078.         new.y = ((double)lnew.y) / fudge;
  1079.         */
  1080.         color = (((double)lsqr(lnew.x))/fudge + ((double)lsqr(lnew.y))/fudge) * (maxit>>1) + 1;
  1081.      }
  1082.      else
  1083.         color = (sqr(new.x) + sqr(new.y)) * (maxit>>1) + 1;
  1084.       }
  1085.       else /* inside == -1 */
  1086.      color = maxit;
  1087.       if (LogTable)
  1088.      color = LogTable[color];
  1089.    }
  1090.  
  1091.    plot_pixel:
  1092.  
  1093.    if (color >= colors) /* don't use color 0 unless from inside/outside */
  1094.       if (colors < 16)
  1095.      color &= andcolor;
  1096.       else
  1097.      color = ((color - 1) % andcolor) + 1;    /* skip color zero */
  1098.  
  1099.    (*plot) (col, row, color);
  1100.  
  1101.    if ((kbdcount -= realcolor) <= 0)
  1102.    {
  1103.       if (check_key())
  1104.      return (-1);
  1105.       kbdcount = max_kbdcount;
  1106.    }
  1107.  
  1108.    return (color);
  1109. }
  1110. #undef green
  1111. #undef yellow
  1112.  
  1113.  
  1114.  
  1115. /**************** standardfractal doodad subroutines *********************/
  1116.  
  1117. static void decomposition()
  1118. {
  1119.    static double cos45       = 0.70710678118654750; /* cos 45    degrees */
  1120.    static double sin45       = 0.70710678118654750; /* sin 45    degrees */
  1121.    static double cos22_5   = 0.92387953251128670; /* cos 22.5    degrees */
  1122.    static double sin22_5   = 0.38268343236508980; /* sin 22.5    degrees */
  1123.    static double cos11_25  = 0.98078528040323040; /* cos 11.25    degrees */
  1124.    static double sin11_25  = 0.19509032201612820; /* sin 11.25    degrees */
  1125.    static double cos5_625  = 0.99518472667219690; /* cos 5.625    degrees */
  1126.    static double sin5_625  = 0.09801714032956060; /* sin 5.625    degrees */
  1127.    static double tan22_5   = 0.41421356237309500; /* tan 22.5    degrees */
  1128.    static double tan11_25  = 0.19891236737965800; /* tan 11.25    degrees */
  1129.    static double tan5_625  = 0.09849140335716425; /* tan 5.625    degrees */
  1130.    static double tan2_8125 = 0.04912684976946725; /* tan 2.8125 degrees */
  1131.    static double tan1_4063 = 0.02454862210892544; /* tan 1.4063 degrees */
  1132.    static long lcos45      ; /* cos 45      degrees */
  1133.    static long lsin45      ; /* sin 45      degrees */
  1134.    static long lcos22_5   ; /* cos 22.5   degrees */
  1135.    static long lsin22_5   ; /* sin 22.5   degrees */
  1136.    static long lcos11_25  ; /* cos 11.25  degrees */
  1137.    static long lsin11_25  ; /* sin 11.25  degrees */
  1138.    static long lcos5_625  ; /* cos 5.625  degrees */
  1139.    static long lsin5_625  ; /* sin 5.625  degrees */
  1140.    static long ltan22_5   ; /* tan 22.5   degrees */
  1141.    static long ltan11_25  ; /* tan 11.25  degrees */
  1142.    static long ltan5_625  ; /* tan 5.625  degrees */
  1143.    static long ltan2_8125 ; /* tan 2.8125 degrees */
  1144.    static long ltan1_4063 ; /* tan 1.4063 degrees */
  1145.    static reset_fudge = -1;
  1146.    int temp = 0;
  1147.    int i;
  1148.    struct lcomplex lalt;
  1149.    struct complex alt;
  1150.    color = 0;
  1151.    if (integerfractal) /* the only case */
  1152.    {
  1153.       if (reset_fudge != fudge)
  1154.       {
  1155.      reset_fudge = fudge;
  1156.      lcos45     = cos45     *fudge;
  1157.      lsin45     = sin45     *fudge;
  1158.      lcos22_5   = cos22_5     *fudge;
  1159.      lsin22_5   = sin22_5     *fudge;
  1160.      lcos11_25  = cos11_25     *fudge;
  1161.      lsin11_25  = sin11_25     *fudge;
  1162.      lcos5_625  = cos5_625     *fudge;
  1163.      lsin5_625  = sin5_625     *fudge;
  1164.      ltan22_5   = tan22_5     *fudge;
  1165.      ltan11_25  = tan11_25     *fudge;
  1166.      ltan5_625  = tan5_625     *fudge;
  1167.      ltan2_8125 = tan2_8125  *fudge;
  1168.      ltan1_4063 = tan1_4063  *fudge;
  1169.       }
  1170.       if (lnew.y < 0)
  1171.       {
  1172.      temp = 2;
  1173.      lnew.y = -lnew.y;
  1174.       }
  1175.  
  1176.       if (lnew.x < 0)
  1177.       {
  1178.      ++temp;
  1179.      lnew.x = -lnew.x;
  1180.       }
  1181.  
  1182.       if (decomp[0] >= 8)
  1183.       {
  1184.      temp <<= 1;
  1185.      if (lnew.x < lnew.y)
  1186.      {
  1187.         ++temp;
  1188.         lalt.x = lnew.x; /* just */
  1189.         lnew.x = lnew.y; /* swap */
  1190.         lnew.y = lalt.x; /* them */
  1191.      }
  1192.  
  1193.      if (decomp[0] >= 16)
  1194.      {
  1195.         temp <<= 1;
  1196.         if (multiply(lnew.x,ltan22_5,bitshift) < lnew.y)
  1197.         {
  1198.            ++temp;
  1199.            lalt = lnew;
  1200.            lnew.x = multiply(lalt.x,lcos45,bitshift) +
  1201.            multiply(lalt.y,lsin45,bitshift);
  1202.            lnew.y = multiply(lalt.x,lsin45,bitshift) -
  1203.            multiply(lalt.y,lcos45,bitshift);
  1204.         }
  1205.  
  1206.         if (decomp[0] >= 32)
  1207.         {
  1208.            temp <<= 1;
  1209.            if (multiply(lnew.x,ltan11_25,bitshift) < lnew.y)
  1210.            {
  1211.           ++temp;
  1212.           lalt = lnew;
  1213.           lnew.x = multiply(lalt.x,lcos22_5,bitshift) +
  1214.               multiply(lalt.y,lsin22_5,bitshift);
  1215.           lnew.y = multiply(lalt.x,lsin22_5,bitshift) -
  1216.               multiply(lalt.y,lcos22_5,bitshift);
  1217.            }
  1218.  
  1219.            if (decomp[0] >= 64)
  1220.            {
  1221.           temp <<= 1;
  1222.           if (multiply(lnew.x,ltan5_625,bitshift) < lnew.y)
  1223.           {
  1224.              ++temp;
  1225.              lalt = lnew;
  1226.              lnew.x = multiply(lalt.x,lcos11_25,bitshift) +
  1227.              multiply(lalt.y,lsin11_25,bitshift);
  1228.              lnew.y = multiply(lalt.x,lsin11_25,bitshift) -
  1229.              multiply(lalt.y,lcos11_25,bitshift);
  1230.           }
  1231.  
  1232.           if (decomp[0] >= 128)
  1233.           {
  1234.              temp <<= 1;
  1235.              if (multiply(lnew.x,ltan2_8125,bitshift) < lnew.y)
  1236.              {
  1237.             ++temp;
  1238.             lalt = lnew;
  1239.             lnew.x = multiply(lalt.x,lcos5_625,bitshift) +
  1240.                 multiply(lalt.y,lsin5_625,bitshift);
  1241.             lnew.y = multiply(lalt.x,lsin5_625,bitshift) -
  1242.                 multiply(lalt.y,lcos5_625,bitshift);
  1243.              }
  1244.  
  1245.              if (decomp[0] == 256)
  1246.              {
  1247.             temp <<= 1;
  1248.             if (multiply(lnew.x,ltan1_4063,bitshift) < lnew.y)
  1249.                if ((lnew.x*ltan1_4063 < lnew.y))
  1250.                   ++temp;
  1251.              }
  1252.           }
  1253.            }
  1254.         }
  1255.      }
  1256.       }
  1257.    }
  1258.    else /* double case */
  1259.    {
  1260.       if (new.y < 0)
  1261.       {
  1262.      temp = 2;
  1263.      new.y = -new.y;
  1264.       }
  1265.       if (new.x < 0)
  1266.       {
  1267.      ++temp;
  1268.      new.x = -new.x;
  1269.       }
  1270.       if (decomp[0] >= 8)
  1271.       {
  1272.      temp <<= 1;
  1273.      if (new.x < new.y)
  1274.      {
  1275.         ++temp;
  1276.         alt.x = new.x; /* just */
  1277.         new.x = new.y; /* swap */
  1278.         new.y = alt.x; /* them */
  1279.      }
  1280.      if (decomp[0] >= 16)
  1281.      {
  1282.         temp <<= 1;
  1283.         if (new.x*tan22_5 < new.y)
  1284.         {
  1285.            ++temp;
  1286.            alt = new;
  1287.            new.x = alt.x*cos45 + alt.y*sin45;
  1288.            new.y = alt.x*sin45 - alt.y*cos45;
  1289.         }
  1290.  
  1291.         if (decomp[0] >= 32)
  1292.         {
  1293.            temp <<= 1;
  1294.            if (new.x*tan11_25 < new.y)
  1295.            {
  1296.           ++temp;
  1297.           alt = new;
  1298.           new.x = alt.x*cos22_5 + alt.y*sin22_5;
  1299.           new.y = alt.x*sin22_5 - alt.y*cos22_5;
  1300.            }
  1301.  
  1302.            if (decomp[0] >= 64)
  1303.            {
  1304.           temp <<= 1;
  1305.           if (new.x*tan5_625 < new.y)
  1306.           {
  1307.              ++temp;
  1308.              alt = new;
  1309.              new.x = alt.x*cos11_25 + alt.y*sin11_25;
  1310.              new.y = alt.x*sin11_25 - alt.y*cos11_25;
  1311.           }
  1312.  
  1313.           if (decomp[0] >= 128)
  1314.           {
  1315.              temp <<= 1;
  1316.              if (new.x*tan2_8125 < new.y)
  1317.              {
  1318.             ++temp;
  1319.             alt = new;
  1320.             new.x = alt.x*cos5_625 + alt.y*sin5_625;
  1321.             new.y = alt.x*sin5_625 - alt.y*cos5_625;
  1322.              }
  1323.  
  1324.              if (decomp[0] == 256)
  1325.              {
  1326.             temp <<= 1;
  1327.             if ((new.x*tan1_4063 < new.y))
  1328.                ++temp;
  1329.              }
  1330.           }
  1331.            }
  1332.         }
  1333.      }
  1334.       }
  1335.    }
  1336.    for (i = 1; temp > 0; ++i)
  1337.    {
  1338.       if (temp & 1)
  1339.      color = (1 << i) - 1 - color;
  1340.       temp >>= 1;
  1341.    }
  1342.    if (decomp[0] == 2)
  1343.       color &= 1;
  1344.    if (colors > decomp[0])
  1345.       color++;
  1346. }
  1347.  
  1348. /******************************************************************/
  1349. /* Continuous potential calculation for Mandelbrot and Julia      */
  1350. /* Reference: Science of Fractal Images p. 190.           */
  1351. /* Special thanks to Mark Peterson for his "MtMand" program that  */
  1352. /* beautifully approximates plate 25 (same reference) and spurred */
  1353. /* on the inclusion of similar capabilities in FRACTINT.      */
  1354. /*                                  */
  1355. /* The purpose of this function is to calculate a color value      */
  1356. /* for a fractal that varies continuously with the screen pixels  */
  1357. /* locations for better rendering in 3D.              */
  1358. /*                                  */
  1359. /* Here "magnitude" is the modulus of the orbit value at          */
  1360. /* "iterations". The potparms[] are user-entered paramters        */
  1361. /* controlling the level and slope of the continuous potential      */
  1362. /* surface. Returns color.  - Tim Wegner 6/25/89          */
  1363. /*                                  */
  1364. /*               -- Change history --              */
  1365. /*                                  */
  1366. /* 09/12/89   - added floatflag support and fixed float underflow */
  1367. /*                                  */
  1368. /******************************************************************/
  1369.  
  1370. static int _fastcall potential(double mag, int iterations)
  1371. {
  1372.    float f_mag,f_tmp,pot;
  1373.    double d_tmp;
  1374.    int i_pot;
  1375.    long l_pot;
  1376.    extern char floatflag;
  1377.  
  1378.    if(iterations < maxit)
  1379.    {
  1380.       pot = i_pot = iterations+2;
  1381.       if(i_pot <= 0 || mag <= 1.0)
  1382.      pot = 0.0;
  1383.       else /* pot = log(mag) / pow(2.0, (double)pot); */
  1384.       {
  1385.      if(i_pot < 120 && !floatflag) /* empirically determined limit of fShift */
  1386.      {
  1387.         f_mag = mag;
  1388.         fLog14(f_mag,f_tmp); /* this SHOULD be non-negative */
  1389.         fShift(f_tmp,(char)-i_pot,pot);
  1390.      }
  1391.      else
  1392.      {
  1393.         d_tmp = log(mag)/(double)pow(2.0,(double)pot);
  1394.         if(d_tmp > FLT_MIN) /* prevent float type underflow */
  1395.            pot = d_tmp;
  1396.         else
  1397.            pot = 0.0;
  1398.      }
  1399.       }
  1400.       /* following transformation strictly for aesthetic reasons */
  1401.       /* meaning of parameters:
  1402.         potparam[0] -- zero potential level - highest color -
  1403.         potparam[1] -- slope multiplier -- higher is steeper
  1404.         potparam[2] -- rqlim value if changeable (bailout for modulus) */
  1405.  
  1406.       if(pot > 0.0)
  1407.       {
  1408.      if(floatflag)
  1409.         pot = (float)sqrt((double)pot);
  1410.      else
  1411.      {
  1412.         fSqrt14(pot,f_tmp);
  1413.         pot = f_tmp;
  1414.      }
  1415.      pot = potparam[0] - pot*potparam[1] - 1.0;
  1416.       }
  1417.       else
  1418.      pot = potparam[0] - 1.0;
  1419.       if(pot < 1.0)
  1420.      pot = 1.0; /* avoid color 0 */
  1421.    }
  1422.    else if(inside >= 0)
  1423.       pot = inside;
  1424.    else /* inside < 0 implies inside=maxit, so use 1st pot param instead */
  1425.       pot = potparam[0];
  1426.  
  1427.    i_pot = (l_pot = pot * 256) >> 8;
  1428.    if(i_pot >= colors)
  1429.    {
  1430.       i_pot = colors - 1;
  1431.       l_pot = 255;
  1432.    }
  1433.  
  1434.    if(pot16bit)
  1435.    {
  1436.       if (dotmode != 11) /* if putcolor won't be doing it for us */
  1437.      writedisk(col+sxoffs,row+syoffs,i_pot);
  1438.       writedisk(col+sxoffs,row+sydots+syoffs,(int)l_pot);
  1439.    }
  1440.  
  1441.    return(i_pot);
  1442. }
  1443.  
  1444.  
  1445. /**************** boundary tracing method *********************/
  1446.  
  1447. static int far *LeftX  = (int far *)NULL;
  1448. static int far *RightX = (int far *)NULL;
  1449. static unsigned repeats;
  1450.  
  1451. static int _fastcall calc_xy(int mx, int my) /* return the color for a pixel */
  1452. {
  1453.  
  1454.    color = getcolor(mx,my); /* see if pixel is black */
  1455.    if (color!=0)        /* pixel is NOT black so we must have already */
  1456.    {              /* calculated its color, so lets skip it    */
  1457.       repeats++;        /* count successive easy ones */
  1458.       return(color);
  1459.    }
  1460.    repeats = 0;     /* we'll have to work for this one wo reset counter */
  1461.  
  1462.    col = mx;
  1463.    row = my;
  1464.    color=(*calctype)();
  1465.    return(color);
  1466. } /* calc_xy function of BTM code */
  1467.  
  1468. static int _fastcall boundary_trace(int C, int R)   /* BTM main function */
  1469. {
  1470.    enum
  1471.        {
  1472.       North, East, South, West
  1473.    }
  1474.    Dir;
  1475.    int C_first, bcolor, low_row, iters;
  1476.    low_row = R;
  1477.    Dir = East;
  1478.    bcolor = color;
  1479.    C_first = C;
  1480.    iters = 0;
  1481.    repeats = 0;
  1482.  
  1483.    /* main loop of BTM inside this loop the boundary is traced on screen! */
  1484.    do
  1485.    {
  1486.       if(--kbdcount<=0)
  1487.       {
  1488.      if(check_key())
  1489.         return(-1);
  1490.      kbdcount=max_kbdcount;
  1491.       }
  1492.       iters++;        /* count times thru loop */
  1493.  
  1494.       if (C < LeftX[R])
  1495.      LeftX[R]  = C; /* to aid in filling polygon later */
  1496.       if (C > RightX[R])
  1497.      RightX[R] = C; /* maintain left and right limits */
  1498.       else
  1499.      if (R==low_row)
  1500.         if (C<=C_first) /* works 99.9% of time! */
  1501.            break;
  1502.       switch (Dir)
  1503.       {
  1504.       case North :
  1505.      if (R > low_row)
  1506.         if(calc_xy(C,R-1)==bcolor)
  1507.         {
  1508.            R--;
  1509.            if (C > ixstart)
  1510.           if(calc_xy(C-1,R)==bcolor)
  1511.           {
  1512.              C--;
  1513.              Dir = West;
  1514.           }
  1515.            break;
  1516.         }
  1517.      Dir = East;
  1518.      break;
  1519.       case East :
  1520.      if (C < ixstop)
  1521.         if(calc_xy(C+1,R)==bcolor)
  1522.         {
  1523.            C++;
  1524.            if (R > low_row)
  1525.           if(calc_xy(C,R-1)==bcolor)
  1526.           {
  1527.              R--;
  1528.              Dir = North;
  1529.           }
  1530.            break;
  1531.         }
  1532.      Dir = South;
  1533.      break;
  1534.       case South :
  1535.      if (R < iystop)
  1536.         if(calc_xy(C,R+1)==bcolor)
  1537.         {
  1538.            R++;
  1539.            if (C < ixstop)
  1540.           if(calc_xy(C+1,R)==bcolor)
  1541.           {
  1542.              C++;
  1543.              Dir = East;
  1544.           }
  1545.            break;
  1546.         }
  1547.      Dir = West;
  1548.      break;
  1549.       case West:
  1550.      if (C > ixstart)
  1551.         if(calc_xy(C-1,R)==bcolor)
  1552.         {
  1553.            C--;
  1554.            if (R < iystop)
  1555.           if(calc_xy(C,R+1)== bcolor)
  1556.           {
  1557.              R++;
  1558.              Dir = South;
  1559.           }
  1560.            break;
  1561.         }
  1562.      Dir = North;
  1563.      break;
  1564.       } /* case */
  1565.    }
  1566.    while (repeats<30000); /* emergency backstop, should never be needed */
  1567.    /* PB, made above very high to allow for resumes;  did some checking
  1568.      of code first, and testing, to confirm that it seems unnecessary */
  1569.    if (iters<4)
  1570.    {
  1571.       LeftX[low_row] = 3000;
  1572.       RightX[low_row] = -3000;
  1573.       if (low_row+1 <= iystop)
  1574.       {
  1575.      LeftX[low_row+1] = 3000;
  1576.      RightX[low_row+1] = -3000;
  1577.       }
  1578.       return(0);  /* no need to fill a polygon of 3 points! */
  1579.    }
  1580.  
  1581.    /* Avoid tracing around whole fractal object */
  1582.    if (iystop+1==ydots)
  1583.       if (LeftX[0]==0)
  1584.      if (RightX[0]==ixstop)
  1585.         if (LeftX[iystop]==0)
  1586.            if (RightX[iystop]==ixstop)
  1587.            {
  1588.           /* clean up in this RARE case or next fills will fail! */
  1589.           for (low_row = 0; low_row <= ydots-1; low_row++)
  1590.           {
  1591.              LeftX[low_row] = 3000;
  1592.              RightX[low_row] = -3000;
  1593.           }
  1594.           return(0);
  1595.            }
  1596.    /* fill in the traced polygon, simple but it works darn well */
  1597.    C = 0;
  1598.    for (R = low_row; R<iystop; R++)
  1599.       if (RightX[R] != -3000)
  1600.       {
  1601.      if((kbdcount-=2)<=0)
  1602.      {
  1603.         if(check_key())
  1604.            return(-1);
  1605.         kbdcount=max_kbdcount;
  1606.      }
  1607.      if(fillcolor > -1)
  1608.         C = fillseg1(LeftX[R], RightX[R],R, bcolor);
  1609.      else
  1610.         C = fillseg(LeftX[R], RightX[R],R, bcolor);
  1611.  
  1612.      LeftX[R]  =  3000;
  1613.      RightX[R] = -3000; /* reset array element */
  1614.       }
  1615.       else if (C!=0) /* this is why C = 0 above! */
  1616.      return(0);
  1617.    return(0);
  1618. } /* BTM function */
  1619.  
  1620. static int _fastcall fillseg1(int LeftX, int RightX, int R,  int bcolor)
  1621. {
  1622.    int blockcolor;
  1623.    int    gcolor;
  1624.    register modeON, C;
  1625.    if(fillcolor >0)
  1626.      blockcolor = fillcolor&(colors-1);
  1627.    else
  1628.      blockcolor = 1;  
  1629.    modeON = 0;
  1630.    for (C = LeftX; C <= RightX; C++)
  1631.    {
  1632.       gcolor=getcolor(C,R);
  1633.       if (modeON!=0 && gcolor==0)
  1634.      (*plot)(C,R,blockcolor); /* show boundary by only filling with color 1 */
  1635.       else
  1636.       {
  1637.      if (gcolor==bcolor) /* TW saved a getcolor here */
  1638.         modeON = 1;
  1639.      else
  1640.         modeON = 0;
  1641.       }
  1642.    }
  1643.    return(C);
  1644. }
  1645.  
  1646. static int _fastcall fillseg(int LeftX, int RightX, int R,  int bcolor)
  1647. {
  1648.    unsigned char *forwards;
  1649.    unsigned char *backwards;
  1650.    register modeON, C;
  1651.    int    gcolor,i;
  1652.    modeON = 0;
  1653.    forwards  = (unsigned char *)decoderline;
  1654.    backwards = (unsigned char *)dstack;
  1655.    modeON = 0;
  1656.    get_line(R,LeftX,RightX,forwards);
  1657.    for (C = LeftX; C <= RightX; C++)
  1658.    {
  1659.       gcolor=forwards[C-LeftX];
  1660.       if (modeON!=0 && gcolor==0)
  1661.      forwards[C-LeftX]=bcolor;
  1662.       else
  1663.       {
  1664.      if (gcolor==bcolor) /* TW saved a getcolor here */
  1665.         modeON = 1;
  1666.      else
  1667.         modeON = 0;
  1668.       }
  1669.    }
  1670.    if(plot==putcolor) /* no symmetry! easy! */
  1671.       put_line(R,LeftX,RightX,forwards);
  1672.    else if(plot==symplot2) /* X-axis symmetry */
  1673.    {
  1674.       put_line(R,   LeftX,RightX,forwards);
  1675.       if ((i=yystop-(R-yystart)) > iystop)
  1676.      put_line(i,LeftX,RightX,forwards);
  1677.    }
  1678.    else if(plot==symplot2J) /* Origin symmetry */
  1679.    {
  1680.       reverse_string(backwards,forwards,RightX-LeftX+1);
  1681.       put_line(R,   LeftX,            RightX,           forwards);
  1682.       if ((i=yystop-(R-yystart)) > iystop)
  1683.      put_line(i,xxstop-(RightX-ixstart),xxstop-(LeftX-ixstart),backwards);
  1684.    }
  1685.    else if(plot==symplot2Y) /* Y-axis symmetry */
  1686.    {
  1687.       reverse_string(backwards,forwards,RightX-LeftX+1);
  1688.       put_line(R,LeftX,          RightX,        forwards);
  1689.       put_line(R,xxstop-(RightX-ixstart),xxstop-(LeftX-ixstart),backwards);
  1690.    }
  1691.    else if(plot==symplot4) /* X-axis and Y-axis symmetry */
  1692.    {
  1693.       reverse_string(backwards,forwards,RightX-LeftX+1);
  1694.       put_line(R,LeftX,             RightX,           forwards);
  1695.       put_line(R,xxstop-(RightX-ixstart),   xxstop-(LeftX-ixstart),backwards);
  1696.       if ((i=yystop-(R-yystart)) > iystop)
  1697.       {
  1698.      put_line(i,LeftX,            RightX,           forwards);
  1699.      put_line(i,xxstop-(RightX-ixstart),xxstop-(LeftX-ixstart),backwards);
  1700.       }
  1701.    }
  1702.    else  /* the other symmetry types are on their own! */
  1703.    {
  1704.       int i;
  1705.       for(i=LeftX;i<=RightX;i++)
  1706.      (*plot)(i,R,forwards[i-LeftX]);
  1707.    }
  1708.    return(C);
  1709. }
  1710.  
  1711. /* copy a string backwards for symmetry functions */
  1712. static void _fastcall reverse_string(char *t, char *s, int len)
  1713. {
  1714.    register i;
  1715.    len--;
  1716.    for(i=0;i<=len;i++)
  1717.       t[i] = s[len-i];
  1718. }
  1719.  
  1720. static int bound_trace_main()
  1721. {
  1722.    long maxrow;
  1723.    maxrow = ((long)ydots)*sizeof(int);
  1724.  
  1725.    if (inside == 0 || outside == 0) {
  1726.       static char far msg[]=
  1727.       {"Boundary tracing cannot be used with inside=0 or outside=0."};
  1728.       stopmsg(0,msg);
  1729.       return(-1);
  1730.       }
  1731.    if (colors < 16) {
  1732.       static char far msg[]=
  1733.       {"Boundary tracing cannot be used with < 16 colors."};
  1734.       stopmsg(0,msg);
  1735.       return(-1);
  1736.       }
  1737.  
  1738.    if((LeftX  = (int far *)farmemalloc(maxrow))==(int far *)NULL)
  1739.       return(-1);
  1740.    else if((RightX  = (int far *)farmemalloc(maxrow))==(int far *)NULL)
  1741.    {
  1742.       farmemfree((char far *)LeftX);
  1743.       return(-1);
  1744.    }
  1745.  
  1746.    for (currow = 0; currow < ydots; currow++)
  1747.    {
  1748.       LeftX[currow] = 3000;
  1749.       RightX[currow] = -3000;
  1750.    }
  1751.  
  1752.    got_status = 2;
  1753.    for (currow = iystart; currow <= iystop; currow++)
  1754.    {
  1755.       for (curcol = ixstart; curcol <= ixstop; curcol++)
  1756.       {
  1757.      if(--kbdcount<=0)
  1758.      {
  1759.         if(check_key())
  1760.         {
  1761.            if (iystop != yystop)
  1762.           iystop = yystop - (currow - yystart); /* allow for sym */
  1763.            add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1764.            farmemfree((char far *)LeftX);
  1765.            farmemfree((char far *)RightX);
  1766.            return(-1);
  1767.         }
  1768.         kbdcount=max_kbdcount;
  1769.      }
  1770.  
  1771.      /* BTM Hook! */
  1772.      color = getcolor(curcol,currow);
  1773.      /* if pixel is BLACK (0) then we haven't done it yet!
  1774.         so first calculate its color and call the routine
  1775.         that will try and trace a polygon if one exists */
  1776.      if (color==0)
  1777.      {
  1778.         reset_periodicity = 1;
  1779.         row = currow;
  1780.         col = curcol;
  1781.         color=(*calctype)();
  1782.         reset_periodicity = 0;
  1783.         boundary_trace(curcol, currow); /* go trace boundary! WHOOOOOOO! */
  1784.      }
  1785.       }
  1786.    }
  1787.    farmemfree((char far *)LeftX);
  1788.    farmemfree((char far *)RightX);
  1789.    return(0);
  1790. } /* end of bound_trace_main */
  1791.  
  1792.  
  1793. /************************ super solid guessing *****************************/
  1794.  
  1795. /*
  1796.    I, Timothy Wegner, invented this solidguessing idea and implemented it in
  1797.    more or less the overall framework you see here.  I am adding this note
  1798.    now in a possibly vain attempt to secure my place in history, because
  1799.    Pieter Branderhorst has totally rewritten this routine, incorporating
  1800.    a *MUCH* more sophisticated algorithm.  His revised code is not only
  1801.    faster, but is also more accurate. Harrumph!
  1802. */
  1803.  
  1804. static int solidguess()
  1805. {
  1806.    int i,x,y,xlim,ylim,blocksize;
  1807.    unsigned int *pfxp0,*pfxp1;
  1808.    unsigned int u;
  1809.  
  1810.    guessplot=(plot!=putcolor && plot!=symplot2 && plot!=symplot2J);
  1811.    /* check if guessing at bottom & right edges is ok */
  1812.    bottom_guess = (plot == symplot2 || (plot == putcolor && iystop+1 == ydots));
  1813.    right_guess    = (plot == symplot2J
  1814.        || ((plot == putcolor || plot == symplot2) && ixstop+1 == xdots));
  1815.  
  1816.    i = maxblock = blocksize = ssg_blocksize();
  1817.    totpasses = 1;
  1818.    while ((i >>= 1) > 1) ++totpasses;
  1819.  
  1820.    /* ensure window top and left are on required boundary, treat window
  1821.      as larger than it really is if necessary (this is the reason symplot
  1822.      routines must check for > xdots/ydots before plotting sym points) */
  1823.    ixstart &= -1 - (maxblock-1);
  1824.    iystart = yybegin;
  1825.    iystart &= -1 - (maxblock-1);
  1826.  
  1827.    got_status = 1;
  1828.  
  1829.    if (workpass == 0) /* otherwise first pass already done */
  1830.    {
  1831.       /* first pass, calc every blocksize**2 pixel, quarter result & paint it */
  1832.       curpass = 1;
  1833.       if (iystart <= yystart) /* first time for this window, init it */
  1834.       {
  1835.      currow = 0;
  1836.      memset(&prefix[1][0][0],0,maxxblk*maxyblk*2); /* noskip flags off */
  1837.      reset_periodicity = 1;
  1838.      row=iystart;
  1839.      for(col=ixstart; col<=ixstop; col+=maxblock)
  1840.      { /* calc top row */
  1841.         if((*calctype)()==-1)
  1842.         {
  1843.            add_worklist(xxstart,xxstop,yystart,yystop,yybegin,0,worksym);
  1844.            goto exit_solidguess;
  1845.         }
  1846.         reset_periodicity = 0;
  1847.      }
  1848.       }
  1849.       else
  1850.      memset(&prefix[1][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  1851.       for(y=iystart; y<=iystop; y+=blocksize)
  1852.       {
  1853.      currow = y;
  1854.      i = 0;
  1855.      if(y+blocksize<=iystop)
  1856.      { /* calc the row below */
  1857.         row=y+blocksize;
  1858.         reset_periodicity = 1;
  1859.         for(col=ixstart; col<=ixstop; col+=maxblock)
  1860.         {
  1861.            if((i=(*calctype)()) == -1)
  1862.           break;
  1863.            reset_periodicity = 0;
  1864.         }
  1865.      }
  1866.      reset_periodicity = 1;
  1867.      if (i == -1 || guessrow(1,y,blocksize) != 0) /* interrupted? */
  1868.      {
  1869.         if (y < yystart)
  1870.            y = yystart;
  1871.         add_worklist(xxstart,xxstop,yystart,yystop,y,0,worksym);
  1872.         goto exit_solidguess;
  1873.      }
  1874.       }
  1875.  
  1876.       if (num_worklist) /* work list not empty, just do 1st pass */
  1877.       {
  1878.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  1879.      goto exit_solidguess;
  1880.       }
  1881.       ++workpass;
  1882.       iystart = yystart & (-1 - (maxblock-1));
  1883.  
  1884.       /* calculate skip flags for skippable blocks */
  1885.       xlim=(ixstop+maxblock)/maxblock+1;
  1886.       ylim=((iystop+maxblock)/maxblock+15)/16+1;
  1887.       if(right_guess==0) /* no right edge guessing, zap border */
  1888.      for(y=0;y<=ylim;++y)
  1889.         prefix[1][y][xlim]=-1;
  1890.       if(bottom_guess==0) /* no bottom edge guessing, zap border */
  1891.       {
  1892.      i=(iystop+maxblock)/maxblock+1;
  1893.      y=i/16+1;
  1894.      i=1<<(i&15);
  1895.      for(x=0;x<=xlim;++x)
  1896.         prefix[1][y][x]|=i;
  1897.       }
  1898.       /* set each bit in prefix[0] to OR of it & surrounding 8 in prefix[1] */
  1899.       for(y=0;++y<ylim;)
  1900.       {
  1901.      pfxp0=&prefix[0][y][0];
  1902.      pfxp1=&prefix[1][y][0];
  1903.      for(x=0;++x<xlim;)
  1904.      {
  1905.         ++pfxp1;
  1906.         u=*(pfxp1-1)|*pfxp1|*(pfxp1+1);
  1907.         *(++pfxp0)=u|(u>>1)|(u<<1)
  1908.            |((*(pfxp1-(maxxblk+1))|*(pfxp1-maxxblk)|*(pfxp1-(maxxblk-1)))>>15)
  1909.           |((*(pfxp1+(maxxblk-1))|*(pfxp1+maxxblk)|*(pfxp1+(maxxblk+1)))<<15);
  1910.      }
  1911.       }
  1912.    }
  1913.    else /* first pass already done */
  1914.       memset(&prefix[0][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  1915.  
  1916.    /* remaining pass(es), halve blocksize & quarter each blocksize**2 */
  1917.    i = workpass;
  1918.    while (--i > 0) /* allow for already done passes */
  1919.       blocksize = blocksize>>1;
  1920.    reset_periodicity = 1;
  1921.    while((blocksize=blocksize>>1)>=2)
  1922.    {
  1923.       curpass = workpass + 1;
  1924.       for(y=iystart; y<=iystop; y+=blocksize)
  1925.       {
  1926.      currow = y;
  1927.      if(guessrow(0,y,blocksize)!=0)
  1928.      {
  1929.         if (y < yystart)
  1930.            y = yystart;
  1931.         add_worklist(xxstart,xxstop,yystart,yystop,y,workpass,worksym);
  1932.         goto exit_solidguess;
  1933.      }
  1934.       }
  1935.       ++workpass;
  1936.       if (num_worklist /* work list not empty, do one pass at a time */
  1937.       && blocksize>2) /* if 2, we just did last pass */
  1938.       {
  1939.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,workpass,worksym);
  1940.      goto exit_solidguess;
  1941.       }
  1942.       iystart = yystart & (-1 - (maxblock-1));
  1943.    }
  1944.  
  1945.    exit_solidguess:
  1946.    return(0);
  1947. }
  1948.  
  1949. #define calcadot(c,x,y) { col=x; row=y; if((c=(*calctype)())==-1) return -1; }
  1950.  
  1951. static int _fastcall guessrow(int firstpass,int y,int blocksize)
  1952. {
  1953.    int x,i,j,color;
  1954.    int xplushalf,xplusblock;
  1955.    int ylessblock,ylesshalf,yplushalf,yplusblock;
  1956.    int       c21,c31,c41;     /* cxy is the color of pixel at (x,y) */
  1957.    int c12,c22,c32,c42;     /* where c22 is the topleft corner of */
  1958.    int c13,c23,c33;        /* the block being handled in current */
  1959.    int       c24,    c44;     /* iteration                  */
  1960.    int guessed23,guessed32,guessed33,guessed12,guessed13;
  1961.    int prev11,fix21,fix31;
  1962.    unsigned int *pfxptr,pfxmask;
  1963.  
  1964.    halfblock=blocksize>>1;
  1965.    i=y/maxblock;
  1966.    pfxptr=&prefix[firstpass][(i>>4)+1][ixstart/maxblock];
  1967.    pfxmask=1<<(i&15);
  1968.    ylesshalf=y-halfblock;
  1969.    ylessblock=y-blocksize; /* constants, for speed */
  1970.    yplushalf=y+halfblock;
  1971.    yplusblock=y+blocksize;
  1972.    prev11=-1;
  1973.    c24=c12=c13=c22=getcolor(ixstart,y);
  1974.    c31=c21=getcolor(ixstart,(y>0)?ylesshalf:0);
  1975.    if(yplusblock<=iystop)
  1976.       c24=getcolor(ixstart,yplusblock);
  1977.    else if(bottom_guess==0)
  1978.       c24=-1;
  1979.    guessed12=guessed13=0;
  1980.  
  1981.    for(x=ixstart; x<=ixstop;)  /* increment at end, or when doing continue */
  1982.    {
  1983.       if((x&(maxblock-1))==0)  /* time for skip flag stuff */
  1984.       {
  1985.      ++pfxptr;
  1986.      if(firstpass==0 && (*pfxptr&pfxmask)==0)  /* check for fast skip */
  1987.      {
  1988.         /* next useful in testing to make skips visible */
  1989.         /*
  1990.                if(halfblock==1)
  1991.                {
  1992.                   (*plot)(x+1,y,0); (*plot)(x,y+1,0); (*plot)(x+1,y+1,0);
  1993.                   }
  1994.              */
  1995.         x+=maxblock;
  1996.         prev11=c31=c21=c24=c12=c13=c22;
  1997.         guessed12=guessed13=0;
  1998.         continue;
  1999.      }
  2000.       }
  2001.  
  2002.       if(firstpass)  /* 1st pass, paint topleft corner */
  2003.      plotblock(0,x,y,c22);
  2004.       /* setup variables */
  2005.       xplusblock=(xplushalf=x+halfblock)+halfblock;
  2006.       if(xplushalf>ixstop)
  2007.       {
  2008.      if(right_guess==0)
  2009.         c31=-1;
  2010.       }
  2011.       else if(y>0)
  2012.      c31=getcolor(xplushalf,ylesshalf);
  2013.       if(xplusblock<=ixstop)
  2014.       {
  2015.      if(yplusblock<=iystop)
  2016.         c44=getcolor(xplusblock,yplusblock);
  2017.      c41=getcolor(xplusblock,(y>0)?ylesshalf:0);
  2018.      c42=getcolor(xplusblock,y);
  2019.       }
  2020.       else if(right_guess==0)
  2021.      c41=c42=c44=-1;
  2022.       if(yplusblock>iystop)
  2023.      c44=(bottom_guess)?c42:-1;
  2024.  
  2025.       /* guess or calc the remaining 3 quarters of current block */
  2026.       guessed23=guessed32=guessed33=1;
  2027.       c23=c32=c33=c22;
  2028.       if(yplushalf>iystop)
  2029.       {
  2030.      if(bottom_guess==0)
  2031.         c23=c33=-1;
  2032.      guessed23=guessed33=-1;
  2033.       }
  2034.       if(xplushalf>ixstop)
  2035.       {
  2036.      if(right_guess==0)
  2037.         c32=c33=-1;
  2038.      guessed32=guessed33=-1;
  2039.       }
  2040.       while(1) /* go around till none of 23,32,33 change anymore */
  2041.       {
  2042.      if(guessed33>0
  2043.          && (c33!=c44 || c33!=c42 || c33!=c24 || c33!=c32 || c33!=c23))
  2044.      {
  2045.         calcadot(c33,xplushalf,yplushalf);
  2046.         guessed33=0;
  2047.      }
  2048.      if(guessed32>0
  2049.          && (c32!=c33 || c32!=c42 || c32!=c31 || c32!=c21
  2050.          || c32!=c41 || c32!=c23))
  2051.      {
  2052.         calcadot(c32,xplushalf,y);
  2053.         guessed32=0;
  2054.         continue;
  2055.      }
  2056.      if(guessed23>0
  2057.          && (c23!=c33 || c23!=c24 || c23!=c13 || c23!=c12 || c23!=c32))
  2058.      {
  2059.         calcadot(c23,x,yplushalf);
  2060.         guessed23=0;
  2061.         continue;
  2062.      }
  2063.      break;
  2064.       }
  2065.  
  2066.       if(firstpass) /* note whether any of block's contents were calculated */
  2067.      if(guessed23==0 || guessed32==0 || guessed33==0)
  2068.         *pfxptr|=pfxmask;
  2069.  
  2070.       if(halfblock>1) /* not last pass, check if something to display */
  2071.      if(firstpass)    /* display guessed corners, fill in block */
  2072.      {
  2073.         if(guessplot)
  2074.         {
  2075.            if(guessed23>0)
  2076.           (*plot)(x,yplushalf,c23);
  2077.            if(guessed32>0)
  2078.           (*plot)(xplushalf,y,c32);
  2079.            if(guessed33>0)
  2080.           (*plot)(xplushalf,yplushalf,c33);
  2081.         }
  2082.         plotblock(1,x,yplushalf,c23);
  2083.         plotblock(0,xplushalf,y,c32);
  2084.         plotblock(1,xplushalf,yplushalf,c33);
  2085.      }
  2086.      else  /* repaint changed blocks */
  2087.      {
  2088.         if(c23!=c22)
  2089.            plotblock(-1,x,yplushalf,c23);
  2090.         if(c32!=c22)
  2091.            plotblock(-1,xplushalf,y,c32);
  2092.         if(c33!=c22)
  2093.            plotblock(-1,xplushalf,yplushalf,c33);
  2094.      }
  2095.  
  2096.       /* check if some calcs in this block mean earlier guesses need fixing */
  2097.       fix21=((c22!=c12 || c22!=c32)
  2098.       && c21==c22 && c21==c31 && c21==prev11
  2099.       && y>0
  2100.       && (x==ixstart || c21==getcolor(x-halfblock,ylessblock))
  2101.       && (xplushalf>ixstop || c21==getcolor(xplushalf,ylessblock))
  2102.       && c21==getcolor(x,ylessblock));
  2103.       fix31=(c22!=c32
  2104.       && c31==c22 && c31==c42 && c31==c21 && c31==c41
  2105.       && y>0 && xplushalf<=ixstop
  2106.       && c31==getcolor(xplushalf,ylessblock)
  2107.       && (xplusblock>ixstop || c31==getcolor(xplusblock,ylessblock))
  2108.       && c31==getcolor(x,ylessblock));
  2109.       prev11=c31; /* for next time around */
  2110.       if(fix21)
  2111.       {
  2112.      calcadot(c21,x,ylesshalf);
  2113.      if(halfblock>1 && c21!=c22)
  2114.         plotblock(-1,x,ylesshalf,c21);
  2115.       }
  2116.       if(fix31)
  2117.       {
  2118.      calcadot(c31,xplushalf,ylesshalf);
  2119.      if(halfblock>1 && c31!=c22)
  2120.         plotblock(-1,xplushalf,ylesshalf,c31);
  2121.       }
  2122.       if(c23!=c22)
  2123.       {
  2124.      if(guessed12)
  2125.      {
  2126.         calcadot(c12,x-halfblock,y);
  2127.         if(halfblock>1 && c12!=c22)
  2128.            plotblock(-1,x-halfblock,y,c12);
  2129.      }
  2130.      if(guessed13)
  2131.      {
  2132.         calcadot(c13,x-halfblock,yplushalf);
  2133.         if(halfblock>1 && c13!=c22)
  2134.            plotblock(-1,x-halfblock,yplushalf,c13);
  2135.      }
  2136.       }
  2137.       c22=c42;
  2138.       c24=c44;
  2139.       c13=c33;
  2140.       c31=c21=c41;
  2141.       c12=c32;
  2142.       guessed12=guessed32;
  2143.       guessed13=guessed33;
  2144.       x+=blocksize;
  2145.    } /* end x loop */
  2146.  
  2147.    if(firstpass==0 || guessplot) return 0;
  2148.  
  2149.    /* paint rows the fast way */
  2150.    for(i=0;i<halfblock;++i)
  2151.    {
  2152.       if((j=y+i)<=iystop)
  2153.      put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2154.       if((j=y+i+halfblock)<=iystop)
  2155.      put_line(j,xxstart,ixstop,&dstack[xxstart+2048]);
  2156.       if(keypressed()) return -1;
  2157.    }
  2158.    if(plot!=putcolor)  /* symmetry, just vertical & origin the fast way */
  2159.    {
  2160.       if(plot==symplot2J) /* origin sym, reverse lines */
  2161.      for(i=(ixstop+xxstart+1)/2;--i>=xxstart;)
  2162.      {
  2163.         color=dstack[i];
  2164.         dstack[i]=dstack[j=ixstop-(i-xxstart)];
  2165.         dstack[j]=color;
  2166.         j+=2048;
  2167.         color=dstack[i+2048];
  2168.         dstack[i+2048]=dstack[j];
  2169.         dstack[j]=color;
  2170.      }
  2171.       for(i=0;i<halfblock;++i)
  2172.       {
  2173.      if((j=yystop-(y+i-yystart))>iystop && j<ydots)
  2174.         put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2175.      if((j=yystop-(y+i+halfblock-yystart))>iystop && j<ydots)
  2176.         put_line(j,xxstart,ixstop,&dstack[xxstart+2048]);
  2177.      if(keypressed()) return -1;
  2178.       }
  2179.    }
  2180.    return 0;
  2181. }
  2182.  
  2183. static void _fastcall plotblock(int buildrow,int x,int y,int color)
  2184. {
  2185.    int i,xlim,ylim;
  2186.    if((xlim=x+halfblock)>ixstop)
  2187.       xlim=ixstop+1;
  2188.    if(buildrow>=0 && guessplot==0) /* save it for later put_line */
  2189.    {
  2190.       if(buildrow==0)
  2191.      for(i=x;i<xlim;++i)
  2192.         dstack[i]=color;
  2193.       else
  2194.      for(i=x;i<xlim;++i)
  2195.         dstack[i+2048]=color;
  2196.       if (x>=xxstart) /* when x reduced for alignment, paint those dots too */
  2197.      return; /* the usual case */
  2198.    }
  2199.    /* paint it */
  2200.    if((ylim=y+halfblock)>iystop)
  2201.    {
  2202.       if(y>iystop)
  2203.      return;
  2204.       ylim=iystop+1;
  2205.    }
  2206.    for(i=x;++i<xlim;)
  2207.       (*plot)(i,y,color); /* skip 1st dot on 1st row */
  2208.    while(++y<ylim)
  2209.       for(i=x;i<xlim;++i)
  2210.      (*plot)(i,y,color);
  2211. }
  2212.  
  2213.  
  2214. /************************* symmetry plot setup ************************/
  2215.  
  2216. static int _fastcall xsym_split(int xaxis_row,int xaxis_between)
  2217. {
  2218.    int i;
  2219.    if ((worksym&0x11) == 0x10) /* already decided not sym */
  2220.       return(1);
  2221.    if ((worksym&1) != 0) /* already decided on sym */
  2222.       iystop = (yystart+yystop)/2;
  2223.    else /* new window, decide */
  2224.    {
  2225.       worksym |= 0x10;
  2226.       if (xaxis_row <= yystart || xaxis_row >= yystop)
  2227.      return(1); /* axis not in window */
  2228.       i = xaxis_row + (xaxis_row - yystart);
  2229.       if (xaxis_between)
  2230.      ++i;
  2231.       if (i > yystop) /* split into 2 pieces, bottom has the symmetry */
  2232.       {
  2233.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2234.         return(1);
  2235.      iystop = xaxis_row - (yystop - xaxis_row);
  2236.      if (!xaxis_between)
  2237.         --iystop;
  2238.      add_worklist(xxstart,xxstop,iystop+1,yystop,iystop+1,workpass,0);
  2239.      yystop = iystop;
  2240.      return(1); /* tell set_symmetry no sym for current window */
  2241.       }
  2242.       if (i < yystop) /* split into 2 pieces, top has the symmetry */
  2243.       {
  2244.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2245.         return(1);
  2246.      add_worklist(xxstart,xxstop,i+1,yystop,i+1,workpass,0);
  2247.      yystop = i;
  2248.       }
  2249.       iystop = xaxis_row;
  2250.       worksym |= 1;
  2251.    }
  2252.    symmetry = 0;
  2253.    return(0); /* tell set_symmetry its a go */
  2254. }
  2255.  
  2256. static int _fastcall ysym_split(int yaxis_col,int yaxis_between)
  2257. {
  2258.    int i;
  2259.    if ((worksym&0x22) == 0x20) /* already decided not sym */
  2260.       return(1);
  2261.    if ((worksym&2) != 0) /* already decided on sym */
  2262.       ixstop = (xxstart+xxstop)/2;
  2263.    else /* new window, decide */
  2264.    {
  2265.       worksym |= 0x20;
  2266.       if (yaxis_col <= xxstart || yaxis_col >= xxstop)
  2267.      return(1); /* axis not in window */
  2268.       i = yaxis_col + (yaxis_col - xxstart);
  2269.       if (yaxis_between)
  2270.      ++i;
  2271.       if (i > xxstop) /* split into 2 pieces, right has the symmetry */
  2272.       {
  2273.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2274.         return(1);
  2275.      ixstop = yaxis_col - (xxstop - yaxis_col);
  2276.      if (!yaxis_between)
  2277.         --ixstop;
  2278.      add_worklist(ixstop+1,xxstop,yystart,yystop,yystart,workpass,0);
  2279.      xxstop = ixstop;
  2280.      return(1); /* tell set_symmetry no sym for current window */
  2281.       }
  2282.       if (i < xxstop) /* split into 2 pieces, left has the symmetry */
  2283.       {
  2284.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2285.         return(1);
  2286.      add_worklist(i+1,xxstop,yystart,yystop,yystart,workpass,0);
  2287.      xxstop = i;
  2288.       }
  2289.       ixstop = yaxis_col;
  2290.       worksym |= 2;
  2291.    }
  2292.    symmetry = 0;
  2293.    return(0); /* tell set_symmetry its a go */
  2294. }
  2295.  
  2296. static void _fastcall setsymmetry(int sym, int uselist) /* set up proper symmetrical plot functions */
  2297. {
  2298.    extern int forcesymmetry;
  2299.    int i;
  2300.    int parmszero;
  2301.    int xaxis_row, yaxis_col;         /* pixel number for origin */
  2302.    int xaxis_between, yaxis_between; /* if axis between 2 pixels, not on one */
  2303.    double ftemp;
  2304.    symmetry = 1;
  2305.    TranspSymmetry = sym;         /* for tp3d.c, MCP 6-1-90 */
  2306.    if(sym == NOPLOT && forcesymmetry == 999)
  2307.    {
  2308.       plot = noplot;
  2309.       return;
  2310.    }
  2311.    /* NOTE: 16-bit potential disables symmetry */
  2312.    /* also any decomp= option and any inversion not about the origin */
  2313.    /* also any rotation other than 180deg and any off-axis stretch */
  2314.    if ((potflag && pot16bit) || (invert && inversion[2] != 0.0)
  2315.        || decomp[0] != 0
  2316.        || xxmin!=xx3rd || yymin!=yy3rd)
  2317.       return;
  2318.    if(sym != XAXIS && sym != XAXIS_NOPARM && inversion[1] != 0.0 && forcesymmetry == 999)
  2319.       return;
  2320.    if(forcesymmetry != 999)
  2321.       sym = forcesymmetry;
  2322.    parmszero = (parm.x == 0.0 && parm.y == 0.0 && useinitorbit != 1);
  2323.    xaxis_row = yaxis_col = -1;
  2324.    if (fabs(yymin+yymax) < fabs(yymin)+fabs(yymax)) /* axis is on screen */
  2325.    {
  2326.       ftemp = (0.0-yymax) / (yymin-yymax) * (ydots-1) + 0.25;
  2327.       xaxis_row = ftemp;
  2328.       xaxis_between = (ftemp - xaxis_row >= 0.5);
  2329.       if (uselist == 0 && (!xaxis_between || (xaxis_row+1)*2 != ydots))
  2330.      xaxis_row = -1; /* can't split screen, so dead center or not at all */
  2331.    }
  2332.    if (fabs(xxmin+xxmax) < fabs(xxmin)+fabs(xxmax)) /* axis is on screen */
  2333.    {
  2334.       ftemp = (0.0-xxmin) / (xxmax-xxmin) * (xdots-1) + 0.25;
  2335.       yaxis_col = ftemp;
  2336.       yaxis_between = (ftemp - yaxis_col >= 0.5);
  2337.       if (uselist == 0 && (!yaxis_between || (yaxis_col+1)*2 != xdots))
  2338.      yaxis_col = -1; /* can't split screen, so dead center or not at all */
  2339.    }
  2340.    switch(sym)         /* symmetry switch */
  2341.    {
  2342.    case XAXIS_NOREAL:     /* X-axis Symmetry (no real param) */
  2343.       if (parm.x != 0.0) break;
  2344.       goto xsym;
  2345.    case XAXIS_NOIMAG:     /* X-axis Symmetry (no imag param) */
  2346.       if (parm.y != 0.0) break;
  2347.       goto xsym;
  2348.    case XAXIS_NOPARM:                 /* X-axis Symmetry  (no params)*/
  2349.       if (!parmszero)
  2350.      break;
  2351.       xsym:
  2352.    case XAXIS:                 /* X-axis Symmetry */
  2353.       if (xsym_split(xaxis_row,xaxis_between) == 0)
  2354.      if(basin)
  2355.         plot = symplot2basin;
  2356.      else
  2357.         plot = symplot2;
  2358.       break;
  2359.    case YAXIS_NOPARM:                 /* Y-axis Symmetry (No Parms)*/
  2360.       if (!parmszero)
  2361.      break;
  2362.    case YAXIS:                 /* Y-axis Symmetry */
  2363.       if (ysym_split(yaxis_col,yaxis_between) == 0)
  2364.      plot = symplot2Y;
  2365.       break;
  2366.    case XYAXIS_NOPARM:                 /* X-axis AND Y-axis Symmetry (no parms)*/
  2367.       if(!parmszero)
  2368.      break;
  2369.    case XYAXIS:              /* X-axis AND Y-axis Symmetry */
  2370.       xsym_split(xaxis_row,xaxis_between);
  2371.       ysym_split(yaxis_col,yaxis_between);
  2372.       switch (worksym & 3)
  2373.       {
  2374.       case 1: /* just xaxis symmetry */
  2375.      if(basin)
  2376.         plot = symplot2basin;
  2377.      else
  2378.         plot = symplot2 ;
  2379.      break;
  2380.       case 2: /* just yaxis symmetry */
  2381.      if (basin) /* got no routine for this case */
  2382.      {
  2383.         ixstop = xxstop; /* fix what split should not have done */
  2384.         symmetry = 1;
  2385.      }
  2386.      else
  2387.         plot = symplot2Y;
  2388.      break;
  2389.       case 3: /* both axes */
  2390.      if(basin)
  2391.         plot = symplot4basin;
  2392.      else
  2393.         plot = symplot4 ;
  2394.       }
  2395.       break;
  2396.    case ORIGIN_NOPARM:                 /* Origin Symmetry (no parms)*/
  2397.       if (!parmszero)
  2398.      break;
  2399.    case ORIGIN:              /* Origin Symmetry */
  2400.       originsym:
  2401.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2402.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2403.       {
  2404.      plot = symplot2J;
  2405.      ixstop = xxstop; /* didn't want this changed */
  2406.       }
  2407.       else
  2408.       {
  2409.      iystop = yystop; /* in case first split worked */
  2410.      symmetry = 1;
  2411.      worksym = 0x30; /* let it recombine with others like it */
  2412.       }
  2413.       break;
  2414.    case PI_SYM_NOPARM:
  2415.       if (!parmszero)
  2416.      break;
  2417.    case PI_SYM:              /* PI symmetry */
  2418.       if(invert && forcesymmetry == 999)
  2419.      goto originsym;
  2420.       plot = symPIplot ;
  2421.       symmetry = 0;
  2422.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2423.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2424.      if(parm.y == 0.0)
  2425.         plot = symPIplot4J; /* both axes */
  2426.      else
  2427.         plot = symPIplot2J; /* origin */
  2428.       else
  2429.       {
  2430.      iystop = yystop; /* in case first split worked */
  2431.      worksym = 0x30;  /* don't mark pisym as ysym, just do it unmarked */
  2432.       }
  2433.       pixelpi = (PI/fabs(xxmax-xxmin))*xdots; /* PI in pixels */
  2434.       if ( (ixstop = xxstart+pixelpi-1 ) > xxstop)
  2435.      ixstop = xxstop;
  2436.       if (plot == symPIplot4J && ixstop > (i = (xxstart+xxstop)/2))
  2437.      ixstop = i;
  2438.       break;
  2439.    default:             /* no symmetry */
  2440.       break;
  2441.    }
  2442. }
  2443.  
  2444.  
  2445. /***************** standalone engine for "test" ********************/
  2446.  
  2447. int test()
  2448. {
  2449.    int startrow,startpass,numpasses;
  2450.    startrow = startpass = 0;
  2451.    if (resuming)
  2452.    {
  2453.       start_resume();
  2454.       get_resume(sizeof(int),&startrow,sizeof(int),&startpass,0);
  2455.       end_resume();
  2456.    }
  2457.    if(teststart()) /* assume it was stand-alone, doesn't want passes logic */
  2458.       return(0);
  2459.    numpasses = (stdcalcmode == '1') ? 0 : 1;
  2460.    for (passes=startpass; passes <= numpasses ; passes++)
  2461.    {
  2462.       for (row = startrow; row <= iystop; row=row+1+numpasses)
  2463.       {
  2464.      register int col;
  2465.      for (col = 0; col <= ixstop; col++)       /* look at each point on screen */
  2466.      {
  2467.         register color;
  2468.         init.y = dy0[row]+dy1[col];
  2469.         init.x = dx0[col]+dx1[row];
  2470.         if(check_key())
  2471.         {
  2472.            testend();
  2473.            alloc_resume(20,1);
  2474.            put_resume(sizeof(int),&row,sizeof(int),&passes,0);
  2475.            return(-1);
  2476.         }
  2477.         color = testpt(init.x,init.y,parm.x,parm.y,maxit,inside);
  2478.         if (color >= colors) /* avoid trouble if color is 0 */
  2479.            if (colors < 16)
  2480.           color &= andcolor;
  2481.            else
  2482.           color = ((color-1) % andcolor) + 1; /* skip color zero */
  2483.         (*plot)(col,row,color);
  2484.         if(numpasses && (passes == 0))
  2485.            (*plot)(col,row+1,color);
  2486.      }
  2487.       }
  2488.       startrow = passes + 1;
  2489.    }
  2490.    testend();
  2491.    return(0);
  2492. }
  2493.  
  2494.  
  2495. /***************** standalone engine for "plasma" ********************/
  2496.  
  2497. static int iparmx;                /* iparmx = parm.x * 16 */
  2498. static int shiftvalue;                /* shift based on #colors */
  2499. extern int max_colors;
  2500. static int recur1=1;
  2501. static int pcolors;
  2502.  
  2503. typedef struct palett
  2504. {
  2505.    unsigned char red;
  2506.    unsigned char green;
  2507.    unsigned char blue;
  2508. }
  2509. Palettetype;
  2510.  
  2511. extern Palettetype dacbox[256];
  2512. static int plasma_check;            /* to limit kbd checking */
  2513.  
  2514. static int _fastcall adjust(int xa,int ya,int x,int y,int xb,int yb)
  2515. {
  2516.    long pseudorandom;
  2517.    pseudorandom = ((long)iparmx)*((rand()-16383));
  2518.    pseudorandom = pseudorandom*(abs(xa-xb)+abs(ya-yb));
  2519.    pseudorandom = pseudorandom >> shiftvalue;
  2520.    pseudorandom = ((getcolor(xa,ya)+getcolor(xb,yb)+1)>>1)+pseudorandom;
  2521.    if (pseudorandom <    1) pseudorandom =   1;
  2522.    if (pseudorandom >= pcolors) pseudorandom = pcolors-1;
  2523.    putcolor(x,y,(int)pseudorandom);
  2524.    return((int)pseudorandom);
  2525. }
  2526.  
  2527.  
  2528. static int _fastcall new_adjust(int xa,int ya,int x,int y,int xb,int yb)
  2529. {
  2530.    long pseudorandom;
  2531.    pseudorandom = ((long)iparmx)*((rand()-16383));
  2532.    pseudorandom = pseudorandom * recur1;
  2533.    pseudorandom = pseudorandom >> shiftvalue;
  2534.    pseudorandom = ((getcolor(xa,ya)+getcolor(xb,yb)+1)>>1)+pseudorandom;
  2535.    if (pseudorandom <    1) pseudorandom =   1;
  2536.    if (pseudorandom >= pcolors) pseudorandom = pcolors-1;
  2537.    putcolor(x,y,(int)pseudorandom);
  2538.    return((int)pseudorandom);
  2539. }
  2540.  
  2541.  
  2542. static int _fastcall new_subD (int x1,int y1,int x2,int y2, int recur)
  2543. {
  2544.  
  2545.    int x,y;
  2546.    int v;
  2547.    int nx1;
  2548.    int nx;
  2549.    int ny1, ny;
  2550.    int i;
  2551.  
  2552.     struct sub {
  2553.     unsigned char t; /* top of stack */
  2554.     int v[16]; /* subdivided value */
  2555.     unsigned char r[16];  /* recursion level */
  2556.     };
  2557.  
  2558.     static struct sub subx, suby;
  2559.  
  2560.     recur1=1;
  2561.     for (i=1;i<=recur;i++)
  2562.     recur1 = recur1 * 2;
  2563.     recur1=320/recur1;
  2564.     suby.t = 2;
  2565.     ny    = suby.v[0] = y2;
  2566.     ny1 = suby.v[2] = y1;
  2567.     suby.r[0] = suby.r[2] = 0;
  2568.     suby.r[1] = 1;
  2569.     y = suby.v[1] = (ny1 + ny) >> 1;
  2570.  
  2571.     while (suby.t >= 1)
  2572.     {
  2573.     if ((++plasma_check & 0x0f) == 1)
  2574.         if(check_key())
  2575.         {
  2576. /*   naah, we don't want to flush this key!!!
  2577.         getch();
  2578. */
  2579.         plasma_check--;
  2580.         return(1);
  2581.         }
  2582.     while (suby.r[suby.t-1] < recur)
  2583.     {
  2584.         /*    1.  Create new entry at top of the stack  */
  2585.  
  2586.         /*    2.  Copy old top value to new top value.
  2587.             This is largest y value.          */
  2588.  
  2589.         /*    3.  Smallest y is now old mid point      */
  2590.  
  2591.         /*    4.  Set new mid point recursion level      */
  2592.  
  2593.         /*    5.  New mid point value is average
  2594.             of largest and smallest          */
  2595.  
  2596.         suby.t++;
  2597.         ny1  = suby.v[suby.t] = suby.v[suby.t-1];
  2598.         ny     = suby.v[suby.t-2];
  2599.         suby.r[suby.t] = suby.r[suby.t-1];
  2600.         y     = suby.v[suby.t-1]   = (ny1 + ny) >> 1;
  2601.         suby.r[suby.t-1]   = (int)max(suby.r[suby.t],
  2602.                         suby.r[suby.t-2])+1;
  2603.     }
  2604.     subx.t = 2;
  2605.     nx  = subx.v[0] = x2;
  2606.     nx1 = subx.v[2] = x1;
  2607.     subx.r[0] = subx.r[2] = 0;
  2608.     subx.r[1] = 1;
  2609.     x = subx.v[1] = (nx1 + nx) >> 1;
  2610.  
  2611.     while (subx.t >= 1)
  2612.     {
  2613.         while (subx.r[subx.t-1] < recur)
  2614.         {
  2615.         subx.t++; /* move the top ofthe stack up 1 */
  2616.         nx1  = subx.v[subx.t] = subx.v[subx.t-1];
  2617.         nx   = subx.v[subx.t-2];
  2618.         subx.r[subx.t] = subx.r[subx.t-1];
  2619.         x    = subx.v[subx.t-1]   = (nx1 + nx) >> 1;
  2620.         subx.r[subx.t-1]   = (int)max(subx.r[subx.t],
  2621.                         subx.r[subx.t-2])+1;
  2622.  
  2623.     }
  2624.  
  2625.        if ((i = getcolor(nx, y)) == 0)
  2626.            i = new_adjust(nx,ny1,nx,y ,nx,ny);
  2627.            v = i;
  2628.        if ((i = getcolor(x, ny)) == 0)
  2629.            i = new_adjust(nx1,ny,x ,ny,nx,ny);
  2630.            v += i;
  2631.        if(getcolor(x,y) == 0)
  2632.        {
  2633.            if ((i = getcolor(x, ny1)) == 0)
  2634.            i = new_adjust(nx1,ny1,x ,ny1,nx,ny1);
  2635.            v += i;
  2636.            if ((i = getcolor(nx1, y)) == 0)
  2637.            i = new_adjust(nx1,ny1,nx1,y ,nx1,ny);
  2638.            v += i;
  2639.            putcolor(x,y,(v + 2) >> 2);
  2640.        }
  2641.  
  2642.        if (subx.r[subx.t-1] == recur) subx.t = subx.t - 2;
  2643.     }
  2644.  
  2645.     if (suby.r[suby.t-1] == recur) suby.t = suby.t - 2;
  2646.     }
  2647.     return(0);
  2648. }
  2649.  
  2650.  
  2651. static void _fastcall subDivide(int x1,int y1,int x2,int y2)
  2652. {
  2653.    int x,y;
  2654.    int v,i;
  2655.  
  2656.    if ((++plasma_check & 0x7f) == 1)
  2657.       if(check_key())
  2658.       {
  2659.      plasma_check--;
  2660.      return;
  2661.       }
  2662.    if(x2-x1<2 && y2-y1<2)
  2663.       return;
  2664.  
  2665.    x = (x1+x2)>>1;
  2666.    y = (y1+y2)>>1;
  2667.    if((v=getcolor(x,y1)) == 0)
  2668.        v=adjust(x1,y1,x ,y1,x2,y1);
  2669.    i=v;
  2670.    if((v=getcolor(x2,y)) == 0)
  2671.        v=adjust(x2,y1,x2,y ,x2,y2);
  2672.    i+=v;
  2673.    if((v=getcolor(x,y2)) == 0)
  2674.        v=adjust(x1,y2,x ,y2,x2,y2);
  2675.    i+=v;
  2676.    if((v=getcolor(x1,y)) == 0)
  2677.        v=adjust(x1,y1,x1,y ,x1,y2);
  2678.    i+=v;
  2679.  
  2680.    if(getcolor(x,y) == 0)
  2681.       putcolor(x,y,(i+2)>>2);
  2682.  
  2683.    subDivide(x1,y1,x ,y);
  2684.    subDivide(x ,y1,x2,y);
  2685.    subDivide(x ,y ,x2,y2);
  2686.    subDivide(x1,y ,x ,y2);
  2687. }
  2688.  
  2689. int plasma()
  2690. {
  2691. int i,k;
  2692.  
  2693.    plasma_check = 0;
  2694.  
  2695.    if(colors < 4) {
  2696. static char far plasmamsg[]={"\
  2697. Plasma Clouds can currently only be run in a 4-or-more-color video\n\
  2698. mode (and color-cycled only on VGA adapters [or EGA adapters in their\n\
  2699. 640x350x16 mode])."};
  2700.       stopmsg(0,plasmamsg);
  2701.       return(-1);
  2702.    }
  2703.    iparmx = param[0] * 8;
  2704.    if (parm.x <= 0.0) iparmx = 16;
  2705.    if (parm.x >= 100) iparmx = 800;
  2706.  
  2707.    if ((!rflag) && param[2] == 1)
  2708.        --rseed;
  2709.    if (param[2] != 0 && param[2] != 1)
  2710.       rseed = param[2];
  2711.  
  2712.    srand(rseed);
  2713.    if (!rflag) ++rseed;
  2714.  
  2715.    if (colors == 256)            /* set the (256-color) palette */
  2716.       set_Plasma_palette();        /* skip this if < 256 colors */
  2717.  
  2718.    if (colors > 16)            /* adjust based on # of colors */
  2719.       shiftvalue = 18;
  2720.    else {
  2721.       if (colors > 4)
  2722.      shiftvalue = 22;
  2723.       else {
  2724.      if (colors > 2)
  2725.         shiftvalue = 24;
  2726.      else
  2727.         shiftvalue = 25;
  2728.       }
  2729.    }
  2730.  
  2731.    pcolors = min(colors, max_colors);
  2732.    putcolor(      0,      0,1+(((rand()/pcolors)*(pcolors-1))>>(shiftvalue-11)));
  2733.    putcolor(xdots-1,      0,1+(((rand()/pcolors)*(pcolors-1))>>(shiftvalue-11)));
  2734.    putcolor(xdots-1,ydots-1,1+(((rand()/pcolors)*(pcolors-1))>>(shiftvalue-11)));
  2735.    putcolor(      0,ydots-1,1+(((rand()/pcolors)*(pcolors-1))>>(shiftvalue-11)));
  2736.  
  2737.    if (param[1] == 0)
  2738.    {
  2739.       subDivide(0,0,xdots-1,ydots-1);
  2740.    }
  2741.    else
  2742.    {
  2743.       recur1 = i = k = 1;
  2744.       while(new_subD(0,0,xdots-1,ydots-1,i)==0)
  2745.       {
  2746.      k = k * 2;
  2747.      if (k    >(int)max(xdots-1,ydots-1))
  2748.          break;
  2749.      if (check_key())
  2750.          return(1);
  2751.      i++;
  2752.       }
  2753.    }
  2754.  
  2755.    if (! check_key())
  2756.       return(0);
  2757.    else
  2758.       return(1);
  2759. }
  2760.  
  2761. static void set_Plasma_palette()
  2762. {
  2763.    extern char far *mapdacbox;
  2764.    static Palettetype Red    = {
  2765.       63, 0, 0     };
  2766.    static Palettetype Green  = {
  2767.       0,63, 0    };
  2768.    static Palettetype Blue   = {
  2769.       0, 0,63    };
  2770.    int i;
  2771.  
  2772.    if (mapdacbox) return;        /* map= specified */
  2773.  
  2774.    dacbox[0].red  = 0 ;
  2775.    dacbox[0].green= 0 ;
  2776.    dacbox[0].blue = 0 ;
  2777.    for(i=1;i<=85;i++)
  2778.    {
  2779.       dacbox[i].red   = (i*Green.red   + (86-i)*Blue.red)/85;
  2780.       dacbox[i].green = (i*Green.green + (86-i)*Blue.green)/85;
  2781.       dacbox[i].blue  = (i*Green.blue  + (86-i)*Blue.blue)/85;
  2782.  
  2783.       dacbox[i+85].red     = (i*Red.red    + (86-i)*Green.red)/85;
  2784.       dacbox[i+85].green = (i*Red.green + (86-i)*Green.green)/85;
  2785.       dacbox[i+85].blue  = (i*Red.blue    + (86-i)*Green.blue)/85;
  2786.  
  2787.       dacbox[i+170].red   = (i*Blue.red   + (86-i)*Red.red)/85;
  2788.       dacbox[i+170].green = (i*Blue.green + (86-i)*Red.green)/85;
  2789.       dacbox[i+170].blue  = (i*Blue.blue  + (86-i)*Red.blue)/85;
  2790.    }
  2791.    SetTgaColors();    /* TARGA 3 June 89  j mclain */
  2792.    spindac(0,1);
  2793. }
  2794.  
  2795.  
  2796. /***************** standalone engine for "diffusion" ********************/
  2797.  
  2798. #define RANDOM(x)  (rand()%(x))
  2799.  
  2800. /* This constant assumes that rand() returns a value from 0 to 32676 */
  2801. #define FOURPI    (long)(4*PI*(double)(1L << 16))
  2802.  
  2803. int diffusion()
  2804. {
  2805.    int xmax,ymax,xmin,ymin;    /* Current maximum coordinates */
  2806.    int border;     /* Distance between release point and fractal */
  2807.    double cosine,sine,angle;
  2808.    long lcosine,lsine;
  2809.    int x,y;
  2810.    extern char floatflag;
  2811.  
  2812.    if (diskvideo)
  2813.    {
  2814.       notdiskmsg();
  2815.       return(-1);
  2816.    }
  2817.  
  2818.    bitshift = 16;
  2819.    fudge = 1L << 16;
  2820.  
  2821.    border = param[0];
  2822.  
  2823.    if (border <= 0)
  2824.       border = 10;
  2825.  
  2826.    srand(rseed);
  2827.    if (!rflag) ++rseed;
  2828.  
  2829.    xmax = xdots / 2 + border;  /* Initial box */
  2830.    xmin = xdots / 2 - border;
  2831.    ymax = ydots / 2 + border;
  2832.    ymin = ydots / 2 - border;
  2833.  
  2834.    if (resuming) /* restore worklist, if we can't the above will stay in place */
  2835.    {
  2836.       start_resume();
  2837.       get_resume(sizeof(int),&xmax,sizeof(int),&xmin,sizeof(int),&ymax,
  2838.          sizeof(int),&ymin,0);
  2839.       end_resume();
  2840.    }
  2841.  
  2842.    putcolor(xdots / 2, ydots / 2,RANDOM(colors-1)+1);  /* Seed point */
  2843.  
  2844.    while (1)
  2845.    {
  2846.       /* Release new point on circle just inside the box */
  2847.  
  2848.       if (floatflag)
  2849.       {
  2850.      angle=2*(double)rand()/(RAND_MAX/PI);
  2851.      FPUsincos(&angle,&sine,&cosine);
  2852.      x = cosine*(xmax-xmin) + xdots;
  2853.      y = sine  *(ymax-ymin) + ydots;
  2854.       }
  2855.       else
  2856.       {
  2857.      SinCos086(multiply((long)rand(),FOURPI,16),&lsine,&lcosine);
  2858.      x = (lcosine*(long)(xmax-xmin) >> 16) + xdots;
  2859.      y = (lsine  *(long)(ymax-ymin) >> 16) + ydots;
  2860.       }
  2861.  
  2862.       x = x >> 1; /* divide by 2 */
  2863.       y = y >> 1;
  2864.  
  2865.       /* Loop as long as the point (x,y) is surrounded by color 0 */
  2866.       /* on all eight sides                      */
  2867.       while((getcolor(x+1,y+1) == 0) && (getcolor(x+1,y) == 0) &&
  2868.       (getcolor(x+1,y-1) == 0) && (getcolor(x  ,y+1) == 0) &&
  2869.       (getcolor(x  ,y-1) == 0) && (getcolor(x-1,y+1) == 0) &&
  2870.       (getcolor(x-1,y) == 0) && (getcolor(x-1,y-1) == 0))
  2871.       {
  2872.      /* Erase moving point */
  2873.      if (show_orbit)
  2874.         putcolor(x,y,0);
  2875.  
  2876.      /* Make sure point is inside the box */
  2877.      if (x==xmax)
  2878.         x--;
  2879.      else if (x==xmin)
  2880.         x++;
  2881.      if (y==ymax)
  2882.         y--;
  2883.      else if (y==ymin)
  2884.         y++;
  2885.  
  2886.      /* Take one random step */
  2887.      x += RANDOM(3) - 1;
  2888.      y += RANDOM(3) - 1;
  2889.  
  2890.      /* Check keyboard */
  2891.      if ((++plasma_check & 0x7f) == 1)
  2892.         if(check_key())
  2893.         {
  2894.            alloc_resume(20,1);
  2895.            put_resume(sizeof(int),&xmax,sizeof(int),&xmin, sizeof(int),&ymax,
  2896.          sizeof(int),&ymin,0);
  2897.            plasma_check--;
  2898.            return 1;
  2899.         }
  2900.  
  2901.      /* Show the moving point */
  2902.      if (show_orbit)
  2903.         putcolor(x,y,RANDOM(colors-1)+1);
  2904.  
  2905.       }
  2906.       putcolor(x,y,RANDOM(colors-1)+1);
  2907.  
  2908.       /* Is point to close to the edge? */
  2909.       if (((x+border)>xmax) || ((x-border)<xmin)
  2910.       || ((y-border)<ymin) || ((y+border)>ymax))
  2911.       {
  2912.      /* Increase box size, but not past the edge of the screen */
  2913.      if (ymin != 1)
  2914.      {
  2915.         ymin--;
  2916.         ymax++;
  2917.      }
  2918.      if (xmin != 1)
  2919.      {
  2920.         xmin--;
  2921.         xmax++;
  2922.      }
  2923.      if ((ymin==1) || (xmin==1))
  2924.         return 0;
  2925.       }
  2926.    }
  2927. }
  2928.  
  2929.  
  2930. /************ standalone engine for "bifurcation" types ***************/
  2931.  
  2932. /***************************************************************/
  2933. /* The following code now forms a generalised Fractal Engine   */
  2934. /* for Bifurcation fractal typeS.  By rights it now belongs in */
  2935. /* CALCFRACT.C, but it's easier for me to leave it here !      */
  2936.  
  2937. /* Original code by Phil Wilson, hacked around by Kev Allen.   */
  2938.  
  2939. /* Besides generalisation, enhancements include Periodicity    */
  2940. /* Checking during the plotting phase (AND halfway through the */
  2941. /* filter cycle, if possible, to halve calc times), quicker    */
  2942. /* floating-point calculations for the standard Verhulst type, */
  2943. /* and new bifurcation types (integer bifurcation, f.p & int   */
  2944. /* biflambda - the real equivalent of complex Lambda sets -    */
  2945. /* and f.p renditions of bifurcations of r*sin(Pi*p), which    */
  2946. /* spurred Mitchel Feigenbaum on to discover his Number).      */
  2947.  
  2948. /* To add further types, extend the fractalspecific[] array in */
  2949. /* usual way, with Bifurcation as the engine, and the name of  */
  2950. /* the routine that calculates the next bifucation generation  */
  2951. /* as the "orbitcalc" routine in the fractalspecific[] entry.  */
  2952.  
  2953. /* Bifurcation "orbitcalc" routines get called once per screen */
  2954. /* pixel column.  They should calculate the next generation    */
  2955. /* from the doubles Rate & Population (or the longs lRate &    */
  2956. /* lPopulation if they use integer math), placing the result   */
  2957. /* back in Population (or lPopulation).  They should return 0  */
  2958. /* if all is ok, or any non-zero value if calculation bailout  */
  2959. /* is desirable (eg in case of errors, or the series tending   */
  2960. /* to infinity).        Have fun !               */
  2961. /***************************************************************/
  2962.  
  2963. #define BIG 100000.0
  2964. #define DEFAULTFILTER 1000     /* "Beauty of Fractals" recommends using 5000
  2965.                    (p.25), but that seems unnecessary. Can
  2966.                    override this value with a nonzero param1 */
  2967.  
  2968. #define SEED 0.66        /* starting value for population */
  2969.  
  2970. static int far *verhulst_array;
  2971. static unsigned int filter_cycles;
  2972. static unsigned int half_time_check;
  2973. static long   lPopulation, lRate;
  2974. static double Population,  Rate;
  2975. static int    mono, outside_x;
  2976. static long   LPI;
  2977.  
  2978. int Bifurcation(void)
  2979. {
  2980.    unsigned long array_size;
  2981.    int row, column;
  2982.    column = 0;
  2983.    if (resuming)
  2984.    {
  2985.       start_resume();
  2986.       get_resume(sizeof(int),&column,0);
  2987.       end_resume();
  2988.    }
  2989.  
  2990.    array_size =  (iystop + 2) * sizeof(int);
  2991.    if ((verhulst_array = (int far *) farmemalloc(array_size)) == NULL)
  2992.    {
  2993.       static char far msg[]={"Insufficient free memory for calculation."};
  2994.       stopmsg(0,msg);
  2995.       return(-1);
  2996.    }
  2997.  
  2998.    LPI = PI * fudge;
  2999.  
  3000.    for (row = 0; row <= iystop+1; row++)
  3001.       verhulst_array[row] = 0;
  3002.  
  3003.    mono = 0;
  3004.    if (colors == 2)
  3005.       mono = 1;
  3006.    if (mono)
  3007.    {
  3008.       if (inside)
  3009.       {
  3010.      outside_x = 0;
  3011.      inside = 1;
  3012.       }
  3013.       else
  3014.      outside_x = 1;
  3015.    }
  3016.  
  3017.    filter_cycles = (parm.x <= 0) ? DEFAULTFILTER : parm.x;
  3018.    half_time_check = FALSE;
  3019.    if (periodicitycheck && maxit < filter_cycles)
  3020.    {
  3021.       filter_cycles = (filter_cycles - maxit + 1) / 2;
  3022.       half_time_check = TRUE;
  3023.    }
  3024.  
  3025.    if (integerfractal)
  3026.       linit.y = ly0[iystop];   /* Y-value of    */
  3027.    else
  3028.       init.y = dy0[iystop];   /* bottom pixels */
  3029.  
  3030.    while (column <= ixstop)
  3031.    {
  3032.       if(check_key())
  3033.       {
  3034.      farmemfree((char far *)verhulst_array);
  3035.      alloc_resume(10,1);
  3036.      put_resume(sizeof(int),&column,0);
  3037.      return(-1);
  3038.       }
  3039.       if (integerfractal)
  3040.      lRate = lx0[column];
  3041.       else
  3042.      Rate = dx0[column];
  3043.       verhulst();     /* calculate array once per column */
  3044.  
  3045.       for (row = iystop+1; row > 0; row--)
  3046.       {
  3047.      int color;
  3048.      color = verhulst_array[row];
  3049.      if(color && mono)
  3050.         color = inside;
  3051.      else if((!color) && mono)
  3052.         color = outside_x;
  3053.      verhulst_array[row] = 0;
  3054.      (*plot)(column,row,color);
  3055.       }
  3056.       column++;
  3057.    }
  3058.    farmemfree((char far *)verhulst_array);
  3059.    return(0);
  3060. }
  3061.  
  3062. static void verhulst()        /* P. F. Verhulst (1845) */
  3063. {
  3064.    unsigned int pixel_row, counter, errors;
  3065.  
  3066.     if (integerfractal)
  3067.        lPopulation = (parm.y == 0) ? SEED * fudge : parm.y * fudge;
  3068.     else
  3069.        Population = (parm.y == 0 ) ? SEED : parm.y;
  3070.  
  3071.    errors = overflow = FALSE;
  3072.  
  3073.    for (counter=0 ; counter < filter_cycles ; counter++)
  3074.    {
  3075.       errors = (*(curfractalspecific->orbitcalc))();
  3076.       if (errors)
  3077.      return;
  3078.    }
  3079.  
  3080.    if (half_time_check) /* check for periodicity at half-time */
  3081.    {
  3082.       Bif_Period_Init();
  3083.       for (counter=0 ; counter < maxit ; counter++)
  3084.       {
  3085.      errors = (*(curfractalspecific->orbitcalc))();
  3086.      if (errors) return;
  3087.      if (periodicitycheck && Bif_Periodic(counter)) break;
  3088.       }
  3089.       if (counter >= maxit)   /* if not periodic, go the distance */
  3090.       {
  3091.      for (counter=0 ; counter < filter_cycles ; counter++)
  3092.      {
  3093.         errors = (*(curfractalspecific->orbitcalc))();
  3094.         if (errors) return;
  3095.      }
  3096.       }
  3097.    }
  3098.  
  3099.    if (periodicitycheck) Bif_Period_Init();
  3100.  
  3101.    for (counter=0 ; counter < maxit ; counter++)
  3102.    {
  3103.       errors = (*(curfractalspecific->orbitcalc))();
  3104.       if (errors) return;
  3105.  
  3106.       /* assign population value to Y coordinate in pixels */
  3107.       if (integerfractal)
  3108.      pixel_row = iystop + 1 - (lPopulation - linit.y) / dely;
  3109.       else
  3110.      pixel_row = iystop + 1 - (int)((Population - init.y) / deltaY);
  3111.  
  3112.       /* if it's visible on the screen, save it in the column array */
  3113.       if (pixel_row <= iystop + 1)
  3114.      verhulst_array[ pixel_row ] ++;
  3115.  
  3116.       if (periodicitycheck && Bif_Periodic(counter))
  3117.       {
  3118.      if (pixel_row <= iystop + 1)
  3119.         verhulst_array[ pixel_row ] --;
  3120.      break;
  3121.       }
  3122.    }
  3123. }
  3124. static    long    lBif_closenuf, lBif_savedpop;    /* poss future use  */
  3125. static    double     Bif_closenuf,    Bif_savedpop;
  3126. static    int     Bif_savedinc,    Bif_savedand;
  3127.  
  3128. static void Bif_Period_Init()
  3129. {
  3130.    Bif_savedinc = 1;
  3131.    Bif_savedand = 1;
  3132.    if (integerfractal)
  3133.    {
  3134.       lBif_savedpop = -1;
  3135.       lBif_closenuf = dely / 8;
  3136.    }
  3137.    else
  3138.    {
  3139.       Bif_savedpop = -1.0;
  3140.       Bif_closenuf = deltaY / 8.0;
  3141.    }
  3142. }
  3143.  
  3144. static int _fastcall Bif_Periodic (time)  /* Bifurcation Population Periodicity Check */
  3145. int time;        /* Returns : 1 if periodicity found, else 0 */
  3146. {
  3147.    if ((time & Bif_savedand) == 0)    /* time to save a new value */
  3148.    {
  3149.       if (integerfractal) lBif_savedpop = lPopulation;
  3150.       else             Bif_savedpop =  Population;
  3151.       if (--Bif_savedinc == 0)
  3152.       {
  3153.      Bif_savedand = (Bif_savedand << 1) + 1;
  3154.      Bif_savedinc = 4;
  3155.       }
  3156.    }
  3157.    else             /* check against an old save */
  3158.    {
  3159.       if (integerfractal)
  3160.       {
  3161.      if (labs(lBif_savedpop-lPopulation) <= lBif_closenuf)
  3162.         return(1);
  3163.       }
  3164.       else
  3165.       {
  3166.      if (fabs(Bif_savedpop-Population) <= Bif_closenuf)
  3167.         return(1);
  3168.       }
  3169.    }
  3170.    return(0);
  3171. }
  3172.  
  3173. /**********************************************************************/
  3174. /*                                                      */
  3175. /* The following are Bifurcation "orbitcalc" routines...              */
  3176. /*                                                      */
  3177. /**********************************************************************/
  3178.  
  3179. int BifurcVerhulst()
  3180.   {
  3181.     Population += Rate * Population * (1 - Population);
  3182.     return (fabs(Population) > BIG);
  3183.   }
  3184.  
  3185. int LongBifurcVerhulst()
  3186.   {
  3187.     ltmp.y = lPopulation - multiply(lPopulation,lPopulation,bitshift);
  3188.     lPopulation += multiply(lRate,ltmp.y,bitshift);
  3189.     return (overflow);
  3190.   }
  3191.  
  3192. int BifurcLambda()
  3193.   {
  3194.     Population = Rate * Population * (1 - Population);
  3195.     return (fabs(Population) > BIG);
  3196.   }
  3197.  
  3198. int LongBifurcLambda()
  3199.   {
  3200.     ltmp.y = lPopulation - multiply(lPopulation,lPopulation,bitshift);
  3201.     lPopulation = multiply(lRate,ltmp.y,bitshift);
  3202.     return (overflow);
  3203.   }
  3204.  
  3205. int BifurcAddSinPi()
  3206.   {
  3207.     Population += Rate * sin(PI*Population);
  3208.     return (fabs(Population) > BIG);
  3209.   }
  3210.  
  3211. int LongBifurcAddSinPi()
  3212.   {
  3213.     ltmp.x = multiply(lPopulation,LPI,bitshift);
  3214.     SinCos086(ltmp.x,<mp.x,<mp.y);
  3215.     lPopulation += multiply(lRate,ltmp.x,bitshift);
  3216.     return (overflow);
  3217.   }
  3218.  
  3219. int BifurcSetSinPi()
  3220.   {
  3221.     Population = Rate * sin(PI*Population);
  3222.     return (fabs(Population) > BIG);
  3223.   }
  3224.  
  3225. int LongBifurcSetSinPi()
  3226.   {
  3227.     ltmp.x = multiply(lPopulation,LPI,bitshift);
  3228.     SinCos086(ltmp.x,<mp.x,<mp.y);
  3229.     lPopulation = multiply(lRate,ltmp.x,bitshift);
  3230.     return (overflow);
  3231.   }
  3232.  
  3233. int BifurcStewart()
  3234.   {
  3235.     Population = (Rate * Population * Population) - 1.0;
  3236.     return (fabs(Population) > BIG);
  3237.   }
  3238.  
  3239. int LongBifurcStewart()
  3240.   {
  3241.     lPopulation = multiply(lPopulation,lPopulation,bitshift);
  3242.     lPopulation = multiply(lPopulation,lRate,       bitshift);
  3243.     lPopulation -= fudge;
  3244.     return (overflow);
  3245.   }
  3246.  
  3247. /* Here Endeth the Generalised Bifurcation Fractal Engine   */
  3248.  
  3249. /* END Phil Wilson's Code (modified slightly by Kev Allen et. al. !) */
  3250.  
  3251.  
  3252. /******************* standalone engine for "popcorn" ********************/
  3253.  
  3254. int popcorn()    /* subset of std engine */
  3255. {
  3256.    int start_row;
  3257.    start_row = 0;
  3258.    if (resuming)
  3259.    {
  3260.       start_resume();
  3261.       get_resume(sizeof(int),&start_row,0);
  3262.       end_resume();
  3263.    }
  3264.    kbdcount=max_kbdcount;
  3265.    plot = noplot;
  3266.    tempsqrx = ltempsqrx = 0; /* PB added this to cover weird BAILOUTs */
  3267.    for (row = start_row; row <= iystop; row++)
  3268.    {
  3269.       reset_periodicity = 1;
  3270.       for (col = 0; col <= ixstop; col++)
  3271.       {
  3272.      if (StandardFractal() == -1) /* interrupted */
  3273.      {
  3274.         alloc_resume(10,1);
  3275.         put_resume(sizeof(int),&row,0);
  3276.         return(-1);
  3277.      }
  3278.      reset_periodicity = 0;
  3279.       }
  3280.    }
  3281.    calc_status = 4;
  3282.    return(0);
  3283. }
  3284.  
  3285. /**************** tesseral method by CJLT begins here*********************/
  3286. /*  reworked by PB for speed and resumeability */
  3287.  
  3288. struct tess { /* one of these per box to be done gets stacked */
  3289.    int x1,x2,y1,y2;     /* left/right top/bottom x/y coords  */
  3290.    int top,bot,lft,rgt;  /* edge colors, -1 mixed, -2 unknown */
  3291. };
  3292.  
  3293. static int tesseral()
  3294. {
  3295.    register struct tess *tp;
  3296.  
  3297.    guessplot = (plot != putcolor && plot != symplot2);
  3298.    tp = (struct tess *)&dstack[0];
  3299.    tp->x1 = ixstart;                  /* set up initial box */
  3300.    tp->x2 = ixstop;
  3301.    tp->y1 = iystart;
  3302.    tp->y2 = iystop;
  3303.  
  3304.    if (workpass == 0) { /* not resuming */
  3305.       tp->top = tessrow(ixstart,ixstop,iystart);     /* Do top row */
  3306.       tp->bot = tessrow(ixstart,ixstop,iystop);      /* Do bottom row */
  3307.       tp->lft = tesscol(ixstart,iystart+1,iystop-1); /* Do left column */
  3308.       tp->rgt = tesscol(ixstop,iystart+1,iystop-1);  /* Do right column */
  3309.       if (check_key()) { /* interrupt before we got properly rolling */
  3310.          add_worklist(xxstart,xxstop,yystart,yystop,yystart,0,worksym);
  3311.          return(-1);
  3312.       }
  3313.    }
  3314.  
  3315.    else { /* resuming, rebuild work stack */
  3316.       int i,mid,curx,cury,xsize,ysize;
  3317.       struct tess *tp2;
  3318.       tp->top = tp->bot = tp->lft = tp->rgt = -2;
  3319.       cury = yybegin & 0xfff;
  3320.       ysize = 1; 
  3321.       i = (unsigned)yybegin >> 12;
  3322.       while (--i >= 0) ysize <<= 1;
  3323.       curx = workpass & 0xfff;
  3324.       xsize = 1; 
  3325.       i = (unsigned)workpass >> 12;
  3326.       while (--i >= 0) xsize <<= 1;
  3327.       while (1) {
  3328.          tp2 = tp;
  3329.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* next divide down middle */
  3330.             if (tp->x1 == curx && (tp->x2 - tp->x1 - 2) < xsize)
  3331.                break;
  3332.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  3333.             if (mid > curx) { /* stack right part */
  3334.                memcpy(++tp,tp2,sizeof(*tp));
  3335.                tp->x2 = mid;
  3336.             }
  3337.             tp2->x1 = mid;
  3338.          }
  3339.          else {                   /* next divide across */
  3340.             if (tp->y1 == cury && (tp->y2 - tp->y1 - 2) < ysize) break;
  3341.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  3342.             if (mid > cury) { /* stack bottom part */
  3343.                memcpy(++tp,tp2,sizeof(*tp));
  3344.                tp->y2 = mid;
  3345.             }
  3346.             tp2->y1 = mid;
  3347.          }
  3348.       }
  3349.    }
  3350.  
  3351.    got_status = 4; /* for tab_display */
  3352.  
  3353.    while (tp >= (struct tess *)&dstack[0]) { /* do next box */
  3354.       curcol = tp->x1; /* for tab_display */
  3355.       currow = tp->y1;
  3356.  
  3357.       if (tp->top == -1 || tp->bot == -1 || tp->lft == -1 || tp->rgt == -1)
  3358.          goto tess_split;
  3359.       /* for any edge whose color is unknown, set it */
  3360.       if (tp->top == -2)
  3361.          tp->top = tesschkrow(tp->x1,tp->x2,tp->y1);
  3362.       if (tp->top == -1)
  3363.          goto tess_split;
  3364.       if (tp->bot == -2)
  3365.          tp->bot = tesschkrow(tp->x1,tp->x2,tp->y2);
  3366.       if (tp->bot != tp->top)
  3367.          goto tess_split;
  3368.       if (tp->lft == -2)
  3369.          tp->lft = tesschkcol(tp->x1,tp->y1,tp->y2);
  3370.       if (tp->lft != tp->top)
  3371.          goto tess_split;
  3372.       if (tp->rgt == -2)
  3373.          tp->rgt = tesschkcol(tp->x2,tp->y1,tp->y2);
  3374.       if (tp->rgt != tp->top)
  3375.          goto tess_split;
  3376.  
  3377.       {  /* all 4 edges are the same color, fill in */
  3378.          int i,j;
  3379.          i = 0;
  3380.          if(fillcolor != 0)
  3381.          {
  3382.          if(fillcolor > 0)
  3383.             tp->top = fillcolor & (colors-1);
  3384.          if (guessplot || (j = tp->x2 - tp->x1 - 1) < 2) { /* paint dots */
  3385.             for (col = tp->x1 + 1; col < tp->x2; col++)
  3386.                for (row = tp->y1 + 1; row < tp->y2; row++) {
  3387.                   (*plot)(col,row,tp->top);
  3388.                   if (++i > 500) {
  3389.                      if (check_key()) goto tess_end;
  3390.                      i = 0;
  3391.                   }
  3392.                }
  3393.          }
  3394.          else { /* use put_line for speed */
  3395.             memset(&dstack[2048],tp->top,j);
  3396.             for (row = tp->y1 + 1; row < tp->y2; row++) {
  3397.                put_line(row,tp->x1+1,tp->x2-1,&dstack[2048]);
  3398.                if (plot != putcolor) /* symmetry */
  3399.                   if ((j = yystop-(row-yystart)) > iystop && j < ydots)
  3400.                      put_line(j,tp->x1+1,tp->x2-1,&dstack[2048]);
  3401.                if (++i > 25) {
  3402.                   if (check_key()) goto tess_end;
  3403.                   i = 0;
  3404.                }
  3405.             }
  3406.          }
  3407.          }
  3408.          --tp;
  3409.       }
  3410.       continue;
  3411.  
  3412.       tess_split:
  3413.       {  /* box not surrounded by same color, sub-divide */
  3414.          int mid,midcolor;
  3415.          struct tess *tp2;
  3416.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* divide down the middle */
  3417.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  3418.             midcolor = tesscol(mid, tp->y1+1, tp->y2-1); /* Do mid column */
  3419.             if (midcolor == -3) goto tess_end;
  3420.             if (tp->x2 - mid > 1) {    /* right part >= 1 column */
  3421.                if (tp->top == -1) tp->top = -2;
  3422.                if (tp->bot == -1) tp->bot = -2;
  3423.                tp2 = tp;
  3424.                if (mid - tp->x1 > 1) { /* left part >= 1 col, stack right */
  3425.                   memcpy(++tp,tp2,sizeof(*tp));
  3426.                   tp->x2 = mid;
  3427.                   tp->rgt = midcolor;
  3428.                }
  3429.                tp2->x1 = mid;
  3430.                tp2->lft = midcolor;
  3431.             }
  3432.             else
  3433.                --tp;
  3434.          }
  3435.          else {                   /* divide across the middle */
  3436.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  3437.             midcolor = tessrow(tp->x1+1, tp->x2-1, mid); /* Do mid row */
  3438.             if (midcolor == -3) goto tess_end;
  3439.             if (tp->y2 - mid > 1) {    /* bottom part >= 1 column */
  3440.                if (tp->lft == -1) tp->lft = -2;
  3441.                if (tp->rgt == -1) tp->rgt = -2;
  3442.                tp2 = tp;
  3443.                if (mid - tp->y1 > 1) { /* top also >= 1 col, stack bottom */
  3444.                   memcpy(++tp,tp2,sizeof(*tp));
  3445.                   tp->y2 = mid;
  3446.                   tp->bot = midcolor;
  3447.                }
  3448.                tp2->y1 = mid;
  3449.                tp2->top = midcolor;
  3450.             }
  3451.             else
  3452.                --tp;
  3453.          }
  3454.       }
  3455.  
  3456.    }
  3457.  
  3458.    tess_end:
  3459.    if (tp >= (struct tess *)&dstack[0]) { /* didn't complete */
  3460.       int i,xsize,ysize;
  3461.       xsize = ysize = 1;
  3462.       i = 2;
  3463.       while (tp->x2 - tp->x1 - 2 >= i) {
  3464.          i <<= 1; 
  3465.          ++xsize; 
  3466.       }
  3467.       i = 2;
  3468.       while (tp->y2 - tp->y1 - 2 >= i) {
  3469.          i <<= 1; 
  3470.          ++ysize; 
  3471.       }
  3472.       add_worklist(xxstart,xxstop,yystart,yystop,
  3473.           (ysize<<12)+tp->y1,(xsize<<12)+tp->x1,worksym);
  3474.       return(-1);
  3475.    }
  3476.    return(0);
  3477.  
  3478. } /* tesseral */
  3479.  
  3480. static int _fastcall tesschkcol(int x,int y1,int y2)
  3481. {
  3482.    int i;
  3483.    i = getcolor(x,++y1);
  3484.    while (--y2 > y1)
  3485.       if (getcolor(x,y2) != i) return -1;
  3486.    return i;
  3487. }
  3488.  
  3489. static int _fastcall tesschkrow(int x1,int x2,int y)
  3490. {
  3491.    int i;
  3492.    i = getcolor(x1,y);
  3493.    while (x2 > x1) {
  3494.       if (getcolor(x2,y) != i) return -1;
  3495.       --x2; 
  3496.    }
  3497.    return i;
  3498. }
  3499.  
  3500. static int _fastcall tesscol(int x,int y1,int y2)
  3501. {
  3502.    int colcolor,i;
  3503.    col = x;
  3504.    row = y1;
  3505.    reset_periodicity = 1;
  3506.    colcolor = (*calctype)();
  3507.    reset_periodicity = 0;
  3508.    while (++row <= y2) { /* generate the column */
  3509.       if ((i = (*calctype)()) < 0) return -3;
  3510.       if (i != colcolor) colcolor = -1;
  3511.    }
  3512.    return colcolor;
  3513. }
  3514.  
  3515. static int _fastcall tessrow(int x1,int x2,int y)
  3516. {
  3517.    int rowcolor,i;
  3518.    row = y;
  3519.    col = x1;
  3520.    reset_periodicity = 1;
  3521.    rowcolor = (*calctype)();
  3522.    reset_periodicity = 0;
  3523.    while (++col <= x2) { /* generate the row */
  3524.       if ((i = (*calctype)()) < 0) return -3;
  3525.       if (i != rowcolor) rowcolor = -1;
  3526.    }
  3527.    return rowcolor;
  3528. }
  3529.  
  3530. /******************* standalone engine for "lyapunov" ********************/
  3531. /*** Roy Murphy [76376,721] ***/
  3532. /*** benchmark 15:05        ***/
  3533. int lyaLength;
  3534. int lyaRxy[18];
  3535.  
  3536. lyapunov () {
  3537.     int count, i, row, col, startrow, lnadjust, filter_cycles;
  3538.     long la, lb, ltemp;
  3539.     double a, b, lyap, total, temp;
  3540.     /* e10=22026.4657948  e-10=0.0000453999297625 */
  3541.  
  3542.       startrow = 0;
  3543.       if (resuming) {
  3544.           start_resume();
  3545.           get_resume(sizeof(int), &startrow, 0);
  3546.           end_resume();
  3547.           }
  3548.       for (row=startrow; row<iystop; row++) {
  3549.            for (col=0; col<ixstop; col++) {
  3550.             if (check_key()) {
  3551.                 alloc_resume(10,1);
  3552.                 put_resume(sizeof(int), &row, 0);
  3553.                 return (-1);
  3554.                 }
  3555.  
  3556.             overflow=FALSE;
  3557.             total = 1.0;
  3558.             lnadjust = 0;
  3559.                (*plot)(col, row, 1);
  3560.  
  3561.             a = dx0[col]+dx1[row];
  3562.             b = dy0[row]+dy1[col];
  3563.             Population = param[1];
  3564.             if ((filter_cycles=param[2])==0)
  3565.                 filter_cycles=maxit/2;
  3566.  
  3567.             for (i=0; i<filter_cycles; i++) {
  3568.                 for (count=0; count<lyaLength; count++) {
  3569.                     Rate = lyaRxy[count]?(a):(b);
  3570.                     if (curfractalspecific->orbitcalc()) {
  3571.                         overflow = TRUE;
  3572.                         goto jumpout;
  3573.                         }
  3574.                     }
  3575.                 }
  3576.  
  3577.             for (i=0; i<maxit/2; i++) {
  3578.                 for (count=0; count<lyaLength; count++) {
  3579.                     Rate = lyaRxy[count]?(a):(b);
  3580.                     if (curfractalspecific->orbitcalc()) {
  3581.                         overflow = TRUE;
  3582.                         goto jumpout;
  3583.                         }
  3584.                     temp = fabs(Rate-2.0*Rate*Population);
  3585.                     if ((total *= (temp))==0) {
  3586.                         lnadjust = 0;
  3587.                         goto jumpout;
  3588.                         }
  3589.                     while (total > 22026.4657948) {
  3590.                         total *= 0.0000453999297625;
  3591.                         lnadjust += 10;
  3592.                         }
  3593.                     while (total < 0.0000453999297625) {
  3594.                         total *= 22026.4657948;
  3595.                         lnadjust -= 10;
  3596.                         }
  3597.                     }
  3598.                    }
  3599.  
  3600. jumpout:    if ( overflow || (total <= 0)
  3601.                 || (temp = log(total)+lnadjust) > 0) {
  3602.                 color = 0;
  3603.                 }
  3604.             else {
  3605.                 if (LogFlag) {
  3606.                     lyap = -temp/((double) lyaLength*i);
  3607.                     }
  3608.                 else
  3609.                     lyap = 1 - exp(temp/((double) lyaLength*i));
  3610.                 color = 1 + (int)(lyap * (colors-1));
  3611.                 }
  3612.             (*plot)(col, row, color);
  3613.             }
  3614.         }
  3615.     return 1;
  3616.     }
  3617.  
  3618. lya_setup () {
  3619.       /* This routine sets up the sequence for forcing the Rate parameter
  3620.           to vary between the two values.  It fills the array lyaRxy[] and
  3621.           sets lyaLength to the length of the sequence.
  3622.  
  3623.           The sequence is coded in the bit pattern in an integer.
  3624.           Briefly, the sequence starts with an A the leading zero bits
  3625.           are ignored and the remaining bit sequence is decoded.  The
  3626.           sequence ends with a B.  Not all possible sequences can be
  3627.           represented in this manner, but every possible sequence is
  3628.           either represented as itself, as a rotation of one of the
  3629.           representable sequences, or as the inverse of a representable
  3630.           sequence (swapping 0s and 1s in the array.)  Sequences that
  3631.           are the rotation and/or inverses of another sequence will generate
  3632.           the same lyapunov exponents.
  3633.  
  3634.           A few examples follow:
  3635.               number    sequence
  3636.                   0        ab
  3637.                   1        aab
  3638.                   2        aabb
  3639.                   3        aaab
  3640.                   4        aabbb
  3641.                   5        aabab
  3642.                   6        aaabb (this is a duplicate of 4, a rotated inverse)
  3643.                   7        aaaab
  3644.                   8        aabbbb    etc.
  3645.      */
  3646.  
  3647.     int i, t;
  3648.  
  3649.  
  3650.     lyaLength = 1;
  3651.     i = (int) param[0];
  3652.     lyaRxy[0] = 1;
  3653.     for (t=15; t>=0; t--)
  3654.         if (i & (1<<t)) break;
  3655.     for (; t>=0; t--)
  3656.         lyaRxy[lyaLength++] = (i & (1<<t)) != 0;
  3657.     lyaRxy[lyaLength++] = 0;
  3658.     return 1;
  3659.  }
  3660.